PDA

View Full Version : Grouped Cells - show and hide groups in code



xltrader100
05-07-2013, 08:00 AM
In Excel 11, I have a single column containing many groups of cells (grouped by Row, using Data/Group and Outline/Group...). There are no overlapping groups. I'm using Application.GoTo to scroll to a particular cell in this column, which may be part of group, and if it is then its group may be expanded or collapsed.

If the group is expanded then there's no problem, but if the group is collapsed then GoTo won't expand it, and the GoTo cell is selected but stays hidden.

If my selected cell is in Row-160, and if the group it belongs to extends from Row-152 to Row-183, then if I expand the group manually with the Recorder (using Data/Group and Outline/Show Group), it gives:

ExecuteExcel4Macro "SHOW.DETAIL(1,183,TRUE,,152)"
which works fine, except that in general I won't know what group my GoTo cell belongs to, or where that group starts and stops, or whether that group is currently expanded or collapsed.

If I try to expand all groups, I run into the same problem, and the recorder gives:

Cells.Select
ExecuteExcel4Macro "SHOW.DETAIL(1,1464,TRUE,,1086)"
ExecuteExcel4Macro "SHOW.DETAIL(1,1085,TRUE,,1080)"
ExecuteExcel4Macro "SHOW.DETAIL(1,1079,TRUE,,740)"
ExecuteExcel4Macro "SHOW.DETAIL(1,739,TRUE,,647)"
- etc for 20 more groups.

So how can I expand a group (or all groups) in code to make the GoTo cell visible?

xltrader100
05-07-2013, 03:11 PM
Well, I found 2 ways that sort of work. They don't do exactly what I want (which is to expand only the group containing the selected cell) but if there are no other suggestions then I'll use one of these.

Method 1 - Expand only the row with the selected cell and leave the rest of that group collapsed.

With the Application.GoTo cell selected but hidden along with its collapsed group, run this:

Selection.RowHeight = Rows(2).RowHeight Row-2 is not part of any group, and is representative of the row height for the rest of the sheet, excluding header cells.

This will make the selected cell visible but it won't show it's group-mates which remain hidden, and since I'd like to see the rest of the group as well, then instead of using this method I plan to go with the following:

Method 2 - found here: http://www.pcreview.co.uk/forums/open-close-grouping-through-vba-t3447261p2.html
This expands all groups on the sheet, and then collapses them again after I'm finished with the GoTo cell.

ActiveSheet.Outline.ShowLevels RowLevels:=1 ' to collapse all groups
' and
ActiveSheet.Outline.ShowLevels RowLevels:=2 ' to expand all groups
The "RowLevels:=2" works here since I'm using only one group level, but if the GoTo cell is buried deeper (up to 8 levels) then use the appropriate number.