VBA Group / Ungroup

Abvlecxe

Board Regular
Joined
Sep 10, 2015
Messages
53
Hello,

I have the below code to toggle my grouped columns in excel to expand and contract (the code doesn't remove the grouping), how do I amend this code so that this only applies to specific columns e.g. A:E and leaves all my other grouped columns
Thanks

Public Function LowestRowGroupLevelDisplayed( _
Optional ByVal Worksheet As Worksheet _
) As Long

Dim column As Range
Dim LowestLevel As Long

If Worksheet Is Nothing Then Set Worksheet = ActiveSheet
For Each column In Worksheet.UsedRange.Rows.EntireColumn
If Not column.Hidden Then
If column.OutlineLevel > LowestLevel Then LowestLevel = column.OutlineLevel
End If
Next column
LowestRowGroupLevelDisplayed = LowestLevel

End Function


Sub testGRp()

If (LowestRowGroupLevelDisplayed = 1) Then
ActiveSheet.Outline.ShowLevels columnLevels:=2

Else

ActiveSheet.Outline.ShowLevels columnLevels:=1


End If

End Sub
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
what does the code look like when you record your actions that you want to emulate
 
Upvote 0
Hi, VBA doesn't record anything when you toggle grouping using the excel grouping buttons.

I don't want to remove / insert grouping i want to toggle the already existing grouped sections between open and closed as i am then assigning the macro to a range of buttons which will then open / close a specific section

what does the code look like when you record your actions that you want to emulate
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,614
Members
452,661
Latest member
Nonhle

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