Search code examples
excelvbahiderows

VBA - Hide individual rows, no range


I am quite new to VBA so this might be a very basic question but somehow I can not find the solution on internet.

I have a Toggle Button which should hide/unhide some rows by clicking. The only thing is that i don't want a range of rows to (un)hide but only a few preselected ones (for example, only row 1, 4, 7 and 9).

I searched the internet but everything seems about ranges of rows instead of individual ones.

Private Sub RWCATSHC()
    With Rows("227:243")      (this part I want it to be just a few selected ones)
        .Select
       .EntireRow.Hidden = Not .EntireRow.Hidden
    End With
End Sub

As described this might be a very basic question but I am new to this so any help would be very appreciated.

Thanks in advance!


Solution

  • This will work:

    Private Sub RWCATSHC()
    
    
         Rows(1).EntireRow.Hidden = Not Rows(1).EntireRow.Hidden
         Rows(4).EntireRow.Hidden = Not Rows(4).EntireRow.Hidden
         Rows(7).EntireRow.Hidden = Not Rows(7).EntireRow.Hidden
         Rows(9).EntireRow.Hidden = Not Rows(9).EntireRow.Hidden
    
    End Sub