Hiding Multiple groups of rows using VBA

Costtx

New Member
Joined
May 15, 2017
Messages
19
I have a spreadsheet where I am trying to make it hide multiple rows based on the values of specific cells. This works for the first set of values:

VBA code used:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 8 And Target.Row = 16 Then
If Target.Value = "No" Then
Application.Rows("17:19").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("17:19").Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub

I have tried duplicating it so I could perform this function with multiple selections in the same sheet, however I do not know how to include it multiple times (I do not have a great working knowledge of VBAs).

I have tried to adjust it to the following:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column = 8 And Target.Row = 34 Then
If Target.Value = "No" Then
Application.Rows("35:37").Select
Application.Selection.EntireRow.Hidden = True
ElseIf Target.Value = "Yes" Then
Application.Rows("35:37").Select
Application.Selection.EntireRow.Hidden = False
End If
End If
End Sub

How would I mix it in?

Thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If I've understood your question correctly...

This should work; shortened the code up a little, too.
Always test on a COPY of your work, first.

If you wish to add more, just insert more "Case" statements.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

With Target
    Select Case .Address
        Case "$H$16"
            If .Value = "No" Then
                Me.Rows("17:19").EntireRow.Hidden = True
            Else
                Me.Rows("17:19").EntireRow.Hidden = False
            End If
        Case "$H$34"
            If .Value = "No" Then
                Me.Rows("35:37").EntireRow.Hidden = True
            Else
                Me.Rows("35:37").EntireRow.Hidden = False
            End If
    End Select
End With
End Sub
 
Last edited:
Upvote 0
Try This:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column <> 8 Then Exit Sub
    Select Case Target.Row
        Case 16
            If Target.Value = "No" Then
                Application.Rows("17:19").EntireRow.Hidden = True
            ElseIf Target.Value = "Yes" Then
                Application.Rows("17:19").EntireRow.Hidden = False
            End If
        Case 34
            If Target.Value = "No" Then
                Application.Rows("35:37").EntireRow.Hidden = True
            ElseIf Target.Value = "Yes" Then
                Application.Rows("35:37").EntireRow.Hidden = False
            End If
    End Select
End Sub
 
Upvote 0
If I've understood your question correctly...

This should work; shortened the code up a little, too.
Always test on a COPY of your work, first.

If you wish to add more, just insert more "Case" statements.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

With Target
    Select Case .Address
        Case "$H$16"
            If .Value = "No" Then
                Me.Rows("17:19").EntireRow.Hidden = True
            Else
                Me.Rows("17:19").EntireRow.Hidden = False
            End If
        Case "$H$34"
            If .Value = "No" Then
                Me.Rows("35:37").EntireRow.Hidden = True
            Else
                Me.Rows("35:37").EntireRow.Hidden = False
            End If
    End Select
End With
End Sub
I'm afraid this did not work
 
Upvote 0
Try This:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20180822
If Target.Column <> 8 Then Exit Sub
    Select Case Target.Row
        Case 16
            If Target.Value = "No" Then
                Application.Rows("17:19").EntireRow.Hidden = True
            ElseIf Target.Value = "Yes" Then
                Application.Rows("17:19").EntireRow.Hidden = False
            End If
        Case 34
            If Target.Value = "No" Then
                Application.Rows("35:37").EntireRow.Hidden = True
            ElseIf Target.Value = "Yes" Then
                Application.Rows("35:37").EntireRow.Hidden = False
            End If
    End Select
End Sub
The first one worked but the second did not.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top