Expand rows based on cell value in VBA - multiple in worksheet

VBA_Newbie_

New Member
Joined
Oct 21, 2019
Messages
2
Hello,

I've been trolling this forum on how to expand grouped rows based on cell value in VBA but haven't been able to get any of the posted codes to work for me.

I have three sections of collapsed grouped rows that I would like to have automatically expand if the cell value changes to "Yes". If the cell value is "No" I would the like rows to remain collapsed. Ideally, I would also like the rows to re-collapse if the cell value changes from "Yes" to "No" or if left blank. (Not as big of a concern as getting the cells to expand initially.)

First Cell: C52
First Range: Rows 54-58

Second Cell: C59
Second Range: 61-64

Third Cell: D65
Third Range: 67-77

Any help is greatly appreciated! :)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi there. Put this code in the worksheet code area -
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$52" Then
        If Target.Value = "Yes" Then
            Rows("54:58").EntireRow.Hidden = False
        ElseIf Target.Value = "No" Or Target.Value = "" Then
            Rows("54:58").EntireRow.Hidden = True
        End If
    ElseIf Target.Address = "$C$59" Then
        If Target.Value = "Yes" Then
            Rows("61:64").EntireRow.Hidden = False
        ElseIf Target.Value = "No"  Or Target.Value = "" Then
            Rows("61:64").EntireRow.Hidden = True
        End If
    ElseIf Target.Address = "$D$65" Then
        If Target.Value = "Yes" Then
            Rows("67:77").EntireRow.Hidden = False
        ElseIf Target.Value = "No" Or Target.Value = "" Then
            Rows("67:77").EntireRow.Hidden = True
        End If
    End If
End Sub
 
Last edited:
Upvote 0
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

The script automatically runs when you enter Yes or No in the Ranges

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  10/21/2019  11:21:16 AM  EDT
On Error GoTo M
'First Group
If Target.Address = Range("C52").Address Then
    Select Case Target.Value
        Case "Yes"
            Rows("54:58").Ungroup
        Case "No"
            Rows("54:58").Group
    End Select
End If
'Second group
If Target.Address = Range("C59").Address Then
    Select Case Target.Value
        Case "Yes"
            Rows("61:64").Ungroup
        Case "No"
            Rows("61:64").Group
    End Select
End If

'Third group
If Target.Address = Range("D65").Address Then
    Select Case Target.Value
        Case "Yes"
            Rows("67:77").Ungroup
        Case "No"
            Rows("67:77").Group
    End Select
End If
Exit Sub
M:
MsgBox "That group may not be grouped"
End Sub
 
Upvote 0
You said:
I've been trolling this forum on how to expand grouped rows

So I assume this means Group or Ungrop

Not Hidden or Unhidden.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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