How to programmatically open or close an Excel grouping pane

Hello, I am developing a class around the Excel object and I would like to expand/close a group of rows or columns programmatically...

Everything that I have tried using the ShowDetail property did not succeed so far and the ActiveSheet.Outline.ShowLevels method either expand or close a specific group placed of grouped row(s) or column(s).
Unfortunately, the Excel macro recorder does not record the action performed when clicking the [+] grouping pane button...

Could anyone give me a pointer or an example of how to programmatically open or close an Excel grouping pane?
Who is Participating?
folderolConnect With a Mentor Commented:
If you assign a summary row in a group to a range, you can use the showdetail property to hide / unhide the child rows for that summary.  I make the summary subtotal the top row, this makes the toggling hide / unhide more appealing. From the snippet, you can see I'm just hardcoding in the row number (18).  The Showdetail property is false if even one row in the group is hidden, so the use of the NOT function as
    tcode.ShowDetail = Not tcode.ShowDetail  
(if its currently false, set to true, if its currently true, set to false)
may not be always reliable, it will depend on your user not hiding rows and columns manually.

Attached is a workbook I did on EE about a year ago for another question.  It demos the working code.


Private Sub CommandButton1_Click()
    Dim tcode As Range
    Set tcode = Rows(18)
    tcode.ShowDetail = Not tcode.ShowDetail    
End Sub

Open in new window

sstampfConnect With a Mentor Commented:
You can use the code which I have provided in the sample below. Let me know if you have any questions/concerns.

sir plusSales ManagementCommented:
use Outline.ShowLevels Method
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

I am sorry sirplus but I have posted the same solution almost 20 min before you.
VorenusAuthor Commented:
Thanks a lot for your answers.
Unfortunately not exactly what I am after. What I am looking for is to open/close a particular group, rather than all the ones belonging to a particular level at once.

If I'm not clear, please do not hesitate to let me know.

Thanks again.
VorenusAuthor Commented:
Thanks a lot folderol, it is exactly what I was looking for, the code works great!!
I gave 50 points to sstampf to thank him for his effort anyway, I hope you don't mind...

Thanks again.
I don't mind at all, glad I could help. Odd how macro recording works / doesn't work, isn't it?
All Courses

From novice to tech pro — start learning today.