I have an Excel sheet where I would like to hide or unhide certain rows depending on the value in another cell.
In Short:
The whole thing should depend on the value in cell C2
, D2
, E2
.
If C2
is blank I would like rows 31 to 40
be hidden, if it is not blank, they need to be visible.
The same for the other three cells, always hiding/unhiding the following 10 rows:
D2 --> rows 41 to 50
E2 --> rows 51 to 60
I tried this code but it is not working and I do not get any error either:
Sub Hide_rows()
If Range("LS!C2") = 0 Then
Rows("31:40").EntireRow.Hidden = True
Else
If Range("LS!D2") = 0 Then
Rows("41:50").EntireRow.Hidden = True
Else
If Range("LS!E2") = 0 Then
Rows("51:60").EntireRow.Hidden = True
Else
If Range("LS!C2") > 0 Then
Rows("31:40").EntireRow.Hidden = False
Else
If Range("LS!D2") > 0 Then
Rows("41:50").EntireRow.Hidden = False
Else
If Range("LS!E2") > 0 Then
Rows("51:60").EntireRow.Hidden = False
Else
End If
End If
End If
End If
End If
End If
End Sub
Thank you!
Put this Worksheet_Change event driven sub procedure in the LS worksheet's private code sheet (right-clcik name tab, View Code), not in a public module code sheet.
Any change made to C2:E2 will trigger this sub procdure and the hidden/unhidden nature of those rows will be re-evaluated.
Looks to me like you just need the following lines:
EDIT as per Chris's point - the following will suffice: