Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically group and ungroup rows or columns

Is there a way to programmatically group / ungroup columns or rows in Excel 2010?

Note:

  • the "manual" command is in Data > Outline > Group / Ungroup
  • in excel 2003, this used to work: someSheet.columns(i).ShowDetail = True / False but it does not work any longer in 2010 for groups (only for pivot tables and subtotals groupings)
  • recording a macro does not produce any code that I could use

More precisely, calling myRange.ShowDetail = True in Excel 2010 does expand a collapsed group, but raises an error if the group is already expanded. And the ShowDetail property returns True, whether the group is expanded or not.

like image 310
assylias Avatar asked Feb 12 '13 17:02

assylias


2 Answers

In Excel 2010, the ShowDetail property always return true for a group, whether it is collapsed or expanded. The Hidden property can be used instead:

'to expand
If myRange.EntireColumn.Hidden Then
    myRange.EntireColumn.ShowDetail = True
End If

'to collapse
If Not myRange.EntireColumn.Hidden Then
    myRange.EntireColumn.ShowDetail = False
End If
like image 66
assylias Avatar answered Oct 04 '22 21:10

assylias


Regarding rows not in pivot tables ... It has NOT been my experience in Excel 2010 that ShowDetail ALWAYS evaluates to True. I thought it did but I didn't realize that I needed to be on the summary row for this property to work as expected. Second of all, I didn't realize the summary row by default is UNDER the grouped rows. Testing for collapsed/expanded became much clearer once I changed that setting to have the summary row above the grouped rows (in the Ribbon: Data > Outline, Show the Outline Dlg Box).

If my selected cell is on the summary row, the ShowDetail evalutes to True if the grouped records are showing, and to False if they are not. The key for me was being on the summary row to see that behavior work this way. Having the child/grouped rows above by default really threw me.

Here's my macro, which dynamically expands and collapses the grouped records tied to the summary row when I select a cell on a summary row. And, it makes my cell in column A bold if the section is expanded. This macro does not run if I've selected more than one cell.

Note that worksheet protection prevents expanding and collapsing groups of cells. My worksheet is protected, so I unprotect the sheets to expand/collapse then reprotect them after. (A possible improvement would be for me to just unprotect/protect just the current sheet instead of all of them.)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'TOGGLE SHOW/HIDE ROW
If Target.Cells.Count = 1 Then
    If (Target.EntireRow.OutlineLevel = 1) And (Target.Offset(1, 0).EntireRow.OutlineLevel = 2) And _
       (Target.Column < 15) Then
            Call Macros.ProtShts(False)
                Target.EntireRow.ShowDetail = Not Target.EntireRow.ShowDetail
                If Target.EntireRow.ShowDetail = True Then
                    Range(Cells(Target.Row, 1), Cells(Target.Row, 14)).Font.Bold = True
                Else
                    Range(Cells(Target.Row, 1), Cells(Target.Row, 14)).Font.Bold = False
                End If
            Call Macros.ProtShts(True)
    End If
End If
End Sub

Remember, I set my summary row to be above the grouped records. If your summary row is below the grouped records (the default) then the offset row reference must be changed to -1, like this:

(Target.Offset(1, 0).EntireRow.OutlineLevel = 2)
like image 39
Dru Avatar answered Oct 04 '22 21:10

Dru