VBA 使用相同的宏按钮展开/折叠行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12863752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA expand/collapse rows with same macro button
提问by user1717622
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.
关于如何运行简单的宏按钮,我们有一个小问题。此按钮的用途有两个:展开一行和折叠一行。
1on pressing the button this VBA command is initiated:
1按下按钮,启动此 VBA 命令:
Sub Macro7()
Rows(7).ShowDetail = True
End Sub
This command expands row 7.
此命令扩展第 7 行。
2on pressing the button again (whilst the row is expanded), this VBA is initiated:
2再次按下按钮(当行被展开时),这个 VBA 被启动:
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
米
回答by Tim Williams
Sub Macro7()
With Rows(7)
.ShowDetail = Not .ShowDetail
End With
End Sub
回答by nutsch
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
回答by Jill
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
回答by Cuinn Herrick
Try using a Command Button (ActiveX Control). Use the button caption to identify the toggle state.
尝试使用命令按钮(ActiveX 控件)。使用按钮标题来标识切换状态。
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
回答by JMK
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