Expand and collapse groups using vba

ken1921

New Member
Joined
Aug 21, 2017
Messages
21
Hi

Is it possible to expand and collapse individual groups using vba i.e. as opposed to expanding or collapsing all groups.

In the following example I would like to have 3 buttons somewhere on the sheet, one for each group, to toggle expand collapse each group one at a time

ruqgxe.png
[/IMG]
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi,

I think the way to go is to either Hide or Unhide the columns. The Outline object does not appear to be much use for this operation. So you could have four buttons: One to hide the first group, one to hide the second group, one to hide the second group and one to show everything.
Code:
Private Sub CommandButton1_Click()
    Columns("B:D").Hidden = True
    Columns("G:I").Hidden = False
    Columns("L:N").Hidden = False
End Sub
Private Sub CommandButton2_Click()
    Columns("B:D").Hidden = False
    Columns("G:I").Hidden = True
    Columns("L:N").Hidden = False
End Sub
Private Sub CommandButton3_Click()
    Columns("B:D").Hidden = False
    Columns("G:I").Hidden = False
    Columns("L:N").Hidden = True
End Sub
Private Sub CommandButton4_Click()
    Columns("B:D").Hidden = False
    Columns("G:I").Hidden = False
    Columns("L:N").Hidden = False
End Sub
An alternative would be to make the buttons toggle. You could do this by asking whether the columns were hidden and then doing the opposite.
Code:
Private Sub CommandButton1_Click()
    If Columns("B:D").Hidden Then Columns("B:D").Hidden = False Else Columns("B:D").Hidden = True
End Sub
Private Sub CommandButton2_Click()
    If Columns("G:I").Hidden Then Columns("G:I").Hidden = False Else Columns("G:I").Hidden = True
End Sub
Private Sub CommandButton3_Click()
    If Columns("L:N").Hidden Then Columns("L:N").Hidden = False Else Columns("L:N").Hidden = True
End Sub


Regards,
 
Upvote 0
An alternative would be to make the buttons toggle.
Code:
Private Sub CommandButton1_Click()
    If Columns("B:D").Hidden Then Columns("B:D").Hidden = False Else Columns("B:D").Hidden = True
End Sub
Alternatively ..
Code:
Private Sub CommandButton1_Click()
   Columns("B:D").Hidden = Not Columns("B:D").Hidden
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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