Is there a way to programmatically group / ungroup columns or rows in Excel 2010?
Note:
someSheet.columns(i).ShowDetail = True / False
but it does not work any longer in 2010 for groups (only for pivot tables and subtotals groupings)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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With