简体   繁体   中英

Hide / Unhide specific group

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM