Looping Macro's with merged cells

  • Hi all!


    I've only just registered today and new to VB macro's and just reading through other peoples posts on here has helped me get as far as I have, but I still need a bit of help.


    I have created a spreadsheet that has a table of 100 rows, but I have had to merge the cells for this so it is really 300 rows. I put a macro in to hide each 3 rows when cell in column C was blank, because these are merged cells I don't think I can put a range into the macro, and I am unsure of how to loop the first macro to repeat every third row.


    Below is an example of the code, these steps go up to Row 316.



    I also have the same looping query about a different macro on the same sheet, this macro is to stamp the date and time into a cell, so when cell G16 - Z16 is changed, the cell 2 rows down is stamped with username and date. I can't seem to write this macro more than once for each row.


    Code
    'Macro to add Date and username into comments'
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng1 As Range
    Set rng1 = Intersect(Range("G16:Z16"), Target)
    If rng1 Is Nothing Then Exit Sub
    Application.EnableEvents = False
    rng1.Offset(2, 0).Value = Date & " - " & Environ("username")
    Application.EnableEvents = True
    End Sub


    Any help would be greatly appreciated!

  • Re: Looping Macro's with merged cells


    Perhaps, for the first part...


    Code
    Sub HideRows() 
        
        Dim lngRow as long 
    
    
        '// Change ending value to suit
        For lngRow = 16 to 49 Step 3
            Rows(cStr(lngrow) &":" & cStr(lngRow + 2)).EntireRow.Hidden =  Range("C" & cStr(lngRow)).Value = 0
        Next
     End Sub


    Code typed freehand and totally untested...


    Not sure what you mean by

    Quote

    I can't seem to write this macro more than once for each row

  • Re: Looping Macro's with merged cells


    Cytop, this works great!


    I was hoping that with a shorter macro, it wouldn't take as long but it seems to take around the same amount of time. It's a lot cleaner though, thank you!

  • Re: Looping Macro's with merged cells


    It's the simplest way - but you could turn screen updating off to speed things up.

  • Re: Looping Macro's with merged cells


    So for the second part, I have tried just duplicating the macro so writing:



    But that didn't work I can do this over 2 rows by using:


    Code
    Dim rng1 As Range
    Set rng1 = Intersect(Range("G16:Z16", "G19:Z19"), Target)
    If rng1 Is Nothing Then Exit Sub
    Application.EnableEvents = False
    rng1.Offset(2, 0).Value = Date & " - " & Environ("username")
    Application.EnableEvents = TrueEnd Sub


    but I can't get this to work for rows G22:Z22 - G313:Z313

  • Re: Looping Macro's with merged cells


    I am planning on activating the first macro from a button on a different sheet, this seems to make it quicker, so speed isn't a massive issue, thank you.

  • Re: Looping Macro's with merged cells


    Trying to understand this one...


    If any cell in the range G16:Z16 is changed, you want to write the Date and User Name to AB16...?


    Same if changes are on Row 19, 22 ... 316.

  • Re: Looping Macro's with merged cells


    Changes made to cell G16:Z16 I would like the username and date putting in cell G18:Z18 (2 cells down from the changed cell)


    Repeated to Rows G313:Z313 - username and date in G315:Z315

  • Re: Looping Macro's with merged cells


    Silly me, misread the Row/Column offset parameters...


    would normally do it... but you mentioned the cells are merged...?

    Quote

    ...but I have had to merge the cells for this


    You cannot have different values in merged cells. The top left cell takes priority...

  • Re: Looping Macro's with merged cells


    That is great! Works like a dream!


    These ones aren't merged, the cells to the left of this are merged.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!