Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide / Unhide specific group

Tags:

excel

vba

I would like to create a button to hide and unhide a specific group with VBA. The code I have will hide or unhide all of the groups in the specified level:

Sub Macro1()
ActiveSheet.Outline.ShowLevels RowLevels:=2
End Sub

Lets say I just want to unhide the second group in the grouping level 2 how can I do that?

like image 386
Alex Avatar asked Oct 26 '25 00:10

Alex


1 Answers

Say the "second group in the grouping level 2" is rows 7-10. Then this code would unhide that group:

ActiveSheet.Rows(10).ShowDetail = True

However, it is probably best to turn it into a toggle function that will hide/unhide a group. The following is a generic toggle function (ToggleOutlineGroup) that will allow you to hide/unhide a group by passing the sheet on which the outline is located and the row. The row can be any row in the group, provided it is not part of another subgroup. I have used the last row of the group for simplicity. The sub foo is an example of how to call the function.

Sub foo()

    ToggleOutlineGroup Sheet1, 10

End Sub


Sub ToggleOutlineGroup(sht As Worksheet, row As Long)

    sht.Rows(row).ShowDetail = Not (sht.Rows(row).ShowDetail)

End Sub
like image 164
J. Garth Avatar answered Oct 28 '25 17:10

J. Garth