+ Reply to Thread
Results 1 to 16 of 16

How to repeat a macro in multiple rows of a sheet?

  1. #1
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Question How to repeat a macro in multiple rows of a sheet?

    Howdy,

    I'm new to the board and new to VBA programming, but I think this should be a very simple questions. The code below works for row 5 of my sheet, but I would like it to work on every subsequent row in the sheet (for like 1000 rows). I assume I need a dim loop, but I can't get it to work. Thanks in advance for the help!


    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False

    If [B5] = "Startup" Then
    [F5, I5:K5].Interior.ColorIndex = 3
    Worksheets("Input Log").Range("F5, I5:K5").ClearContents

    Else
    [F5, I5:K5].Interior.ColorIndex = 0
    End If

    If [B5] = "Shutdown" Then
    [D5:E5].Interior.ColorIndex = 3
    Worksheets("Input Log").Range("D5:E5").ClearContents

    Else
    [D5:E5].Interior.ColorIndex = 0
    End If

    If [I5] = "Catalyst Malfunction" Or [I5] = "" Then
    [M5].Interior.ColorIndex = 0

    Else
    [M5].Interior.ColorIndex = 3
    Worksheets("Input Log").Range("M5").ClearContents
    End If

    Application.EnableEvents = True
    End Sub
    Last edited by rgoodman; 07-01-2013 at 09:08 AM.

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: How to repeat a macro in multiple rows of a sheet?

    Hi and welcome to the forums,

    hard to say without test data but try (untested):
    Please Login or Register  to view this content.
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Question Re: How to repeat a macro in multiple rows of a sheet?

    I appreciate the help. Since I'm new to this, I'm not really sure what the code you gave me is doing, but now none of the rows are working properly. I've attached my spreadsheet with my original code--hopefully that helps. Please let me know if you have any questions
    Attached Files Attached Files
    Last edited by rgoodman; 06-26-2013 at 10:17 AM.

  4. #4
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Talking Re: How to repeat a macro in multiple rows of a sheet?

    TTT, hoping for help

  5. #5
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: How to repeat a macro in multiple rows of a sheet?

    Hi,

    try this sample: Worksheet.xlsm

    contains:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Re: How to repeat a macro in multiple rows of a sheet?

    Thanks tehneXus, it's almost there now! The only issue I have now is that I would like for it to still clear the contents in the red cells if the user tries to input anything in them; it's not doing that anymore

  7. #7
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Re: How to repeat a macro in multiple rows of a sheet?

    It clears the contents if they are typed in before the selection is made, but not if the user types something in after the selection is made; I'd like it to do both

  8. #8
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: How to repeat a macro in multiple rows of a sheet?

    try
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Re: How to repeat a macro in multiple rows of a sheet?

    Thanks again for the help. The issue with the most recent code is that after the startup or shutdown selection is made in the first (fifth) row, the same boxes are set to red in each subsequent row. That is, if you select startup in the first row, every row after that has the same boxes set to red, regardless of if startup or shutdown is selected. Also, if "startup" is selected, "what corrective actions were taken?" needs to be set to red.

    Sorry if I'm being a pain--your help is greatly appreciated

  10. #10
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: How to repeat a macro in multiple rows of a sheet?

    Hi,

    that was a mistake from my side, should not post here when I'm drunk, anyway, the following code will check/clear/change-color in the row which is just changed depending on values in column B and I:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Re: How to repeat a macro in multiple rows of a sheet?

    One last thing: if "startup" is selected, "what corrective actions were taken?" needs to be set to red.

    I tried to add ".Cells(i, 13)" to this part
    Case "Startup"
    With Union(.Cells(i, 6), .Range(.Cells(i, 9), .Cells(i, 11)))
    .Interior.ColorIndex = 3
    .ClearContents
    but it didn't help...

  12. #12
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: How to repeat a macro in multiple rows of a sheet?

    Hi,

    Column M is changed depending on the value in column I, how should the logic be then?

  13. #13
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Re: How to repeat a macro in multiple rows of a sheet?

    If "startup" is selected in column B, column M should be set to red and the data cleared. Also, if "shutdown" is selected in column B, column M should be set to red and the data cleared if anything except "catalyst malfunction" is selected in column I. If "catalyst malfunction" is selected in column I, column M should remain available. Let me know if you have any other questions

  14. #14
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Re: How to repeat a macro in multiple rows of a sheet?

    Does that answer your question?

  15. #15
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: How to repeat a macro in multiple rows of a sheet?

    try
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    06-24-2013
    Location
    Texas
    MS-Off Ver
    MS Office 2007
    Posts
    17

    Re: How to repeat a macro in multiple rows of a sheet?

    Perfect, thank you so much! Now I need to try doing some things to track inputs, but I'm going to try to figure it out myself. I may post some questions if I can't figure it out, but I'll start a new topic since they won't be related to the questions in this thread. Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1