可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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