Forum Discussion
VBAs in excel for beginners!
Just repeat the code from If to End if , though with a different cell to check and different rows to hide or unhide.
Hi, thanks I've added them all but the first rule is not working it only hides 3 rows to the next rule when it should hide all questions to line 45 - all other rules are working correctly.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("E5") = "No" Then
Rows("6:45").EntireRow.Hidden = True
Else
Rows("6:45").EntireRow.Hidden = False
End If
If Range("E7") = "No" Then
Rows("8:18").EntireRow.Hidden = True
Else
Rows("8:18").EntireRow.Hidden = False
End If
If Range("E20") = "No" Then
Rows("21:24").EntireRow.Hidden = True
Else
Rows("21:24").EntireRow.Hidden = False
End If
If Range("E26") = "No" Then
Rows("27:36").EntireRow.Hidden = True
Else
Rows("27:36").EntireRow.Hidden = False
End If
If Range("E38") = "No" Then
Rows("39:45").EntireRow.Hidden = True
Else
Rows("39:45").EntireRow.Hidden = False
End If
End Sub
- Riny_van_EekelenJul 01, 2020Platinum Contributor
North_Yorks Insert "Exit Sub" before the first "Else". Then it works. When E5 = No, you want to hide all. No need to check any further. Without the exit code, the macro check the next condition. It is not No, thus it unhides the rows that were hidden in the first step and so on.
Private Sub Worksheet_Change(ByVal Target As Range) If Range("E5") = "No" Then Rows("6:45").EntireRow.Hidden = True Exit Sub Else Rows("6:45").EntireRow.Hidden = False End If If Range("E7") = "No" Then Rows("8:18").EntireRow.Hidden = True Else Rows("8:18").EntireRow.Hidden = False End If If Range("E20") = "No" Then Rows("21:24").EntireRow.Hidden = True Else Rows("21:24").EntireRow.Hidden = False End If If Range("E26") = "No" Then Rows("27:36").EntireRow.Hidden = True Else Rows("27:36").EntireRow.Hidden = False End If If Range("E38") = "No" Then Rows("39:45").EntireRow.Hidden = True Else Rows("39:45").EntireRow.Hidden = False End If End Sub- North_YorksJul 01, 2020Copper Contributor
Riny_van_Eekelen Thank you it now works perfectly!
- Riny_van_EekelenJul 01, 2020Platinum Contributor
North_Yorks Great! Glad I could help solving your problem.