Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA expand/collapse rows with same macro button

Tags:

excel

vba

We have a trivial problem regarding how to run a simple macro button. The purpose of this button is two-fold: expanding a row and collapsing a row.

1 on pressing the button this VBA command is initiated:

Sub Macro7()

Rows(7).ShowDetail = True

End Sub

This command expands row 7.

2 on pressing the button again (whilst the row is expanded), this VBA is initiated:

Sub Macro7()

Rows(7).ShowDetail = False

End Sub

This collapses the row.

Is there a way to link a button to two macros?

Thanks in advance!!!

M

like image 665
user1717622 Avatar asked Oct 12 '12 16:10

user1717622


People also ask

Can you have multiple subs in one macro?

Answers. You can add as many lines as you need. Execute the RunAll macro to run all macros mentioned in it. Yes, the subs should be below each other.

How do I collapse all rows in a pivot table?

Right-click the item, click Expand/Collapse, and then do one of the following: To see the details for the current item, click Expand. To hide the details for the current item, click Collapse. To hide the details for all items in a field, click Collapse Entire Field.

How to expand and collapse rows in Excel?

You can also collapse rows using the Hide Detail command. To do this select the group of rows that you want to collapse. Now, go to the Data tab in the ribbon and click on Hide Detail. That will eventually collapse rows. To expand rows in excel, We can also apply two methods.

Is it possible to run a macro to collapse or expand worksheets?

I'd like to be able to run a macro which would collapse or expand all grouped rows and columns for all selected worksheets. Click to expand... selected worksheets that Domenic provided you in your recent post. The result for Expand_All would look like this. You can adapt the other variations the same way. Thank you!! This is so helpful.

How to expand/collapse the entire Pivot table in Excel 2016?

However, there are no dedicated buttons on the pivot table to expand/collapse the entire field. In Excel 2016 a new feature was added to pivot charts that allows us to expand or collapse the fields in the Rows area. You will see little plus and minus button in the bottom right corner of your pivot chart.

What are the keyboard shortcuts to expand and collapse a field?

Keyboard shortcuts to Expand/Collapse fields: Expand Entire Field: Alt, A, J Or Menu Key, E, E Collapse Entire Field: Alt, A, H Or Menu Key, E, C Note: A cell in the Rows or Columns area must be selected for the keyboard shortcuts to work.


2 Answers

Sub Macro7()  
    With Rows(7)
        .ShowDetail = Not .ShowDetail
    End With
End Sub 
like image 89
Tim Williams Avatar answered Nov 09 '22 04:11

Tim Williams


No need to. Just adjust your macro to check the current state of your row (collapsed or expanded) and act accordingly:

Sub ExpandOrCollapse()

Rows(7).ShowDetail=IIF(Rows(7).ShowDetail,False,true)

End Sub
like image 31
nutsch Avatar answered Nov 09 '22 03:11

nutsch