VBA expand/collapse rows with same macro button

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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

回答1:

Sub Macro7()       With Rows(7)         .ShowDetail = Not .ShowDetail     End With End Sub  


回答2:

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 


回答3:

I tried above answers and it didn't work for me. Below is the code that works:

Sub rowExpanded()  Rows("7:7").Select Selection.EntireRow.Hidden = IIf(Selection.EntireRow.Hidden, False, True)  End Sub 


回答4:

Try this

Dim rowExpanded As Boolean rowExpanded = Rows(7).ShowDetail  If rowExpanded = True Then     Rows(7).ShowDetail = False Else     Rows(7).ShowDetail = True End If 


回答5:

Try using a Command Button (ActiveX Control). Use the button caption to identify the toggle state.

Private Sub CommandButton1_Click()      If CommandButton1.Caption = "-" Then         ActiveSheet.Outline.ShowLevels Rowlevels:=1, ColumnLevels:=1         JobDescriptionToggleButton.Caption = "+"     Else         ActiveSheet.Outline.ShowLevels Rowlevels:=8, ColumnLevels:=8         JobDescriptionToggleButton.Caption = "-"     End If  End Sub 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!