Link to home
Start Free TrialLog in
Avatar of jnash67
jnash67

asked on

Excel VBA Trap the Group and Outline expand/collapse (Show Detail / Hide Detail) events

How can I determine when the user has hit the [+] or [-] buttons in a grouping in a worksheet?
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

There is no specific event for that. If you have a subtotal formula that refers to the hidden/expanded rows, then you could get a calculate event triggered, but you would have no way of knowing for sure what had triggered it.
Regards,
Rory
ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As Rory stated above, the only event you can trigger with a group collapse/expand action is the calculation event. To do so you need to use the SUBTOTAL function which is aware of whether or not rows are hidden. It is not aware of a column's visibility so this solution only works with grouped rows. To capture a collapse/expand action place a SUBTOTAL function anywhere on the worksheet that references the rows being monitored. For example, to monitor all rows use the formula below.

   =SUBTOTAL(1,A:A)

To monitor rows 2 through 100 use the range A2:A100 instead of A:A. Anytime any of the rows in the specified range are collapsed or expanded the formula is recalculated and a calculation event is generated. To determine which group is visible and which is not, query the Hidden property of any row in each group as illustrated below.

   If Rows(10).Hidden Then
      MsgBox "Group is collapsed"
   Else
      MsgBox "Group is expanded"
   End If

Kevin
Avatar of jnash67
jnash67

ASKER

Kevin, your first solution is exactly what I needed.  I couldn't go the subtotal route because I don't have calculation set to automatic for the application in question.