[英]Excel VBA to change the caption on an Excel command button
我正在寻找Excel VBA来快速更改Excel命令按钮上的标题。 默认标题应为“显示差异”,并且在应用过滤器时应更改为“全部显示”。
到目前为止,这就是我所拥有的。
Sub ShowDifference()
Dim cmdButton As CommandButton
'在这里断裂
Set cmdButton = ActiveSheet.Shapes("cmdShowDif")
If cmdButton.Caption = "Show Difference" Then
ActiveSheet.ListObjects("qryDifference").Range.AutoFilter Field:=4, _
Criteria1:=Array("<>0.00"), Operator:=xlAnd
cmdButton.Caption = "Show All"
Else
ActiveSheet.ListObjects("qryDifference").Range.AutoFilter Field:=4
cmdButton.Caption = "Show Difference"
End If
End Sub
它以子名称命名。 为什么?
这是工作代码:
Sub ShowDifference()
Dim cmdButton As Button
Set cmdButton = ActiveSheet.Buttons("cmdShowDif")
If cmdButton.Caption = "Show Difference" Then
cmdButton.Caption = "Show All"
Else
cmdButton.Caption = "Show Difference"
End If
End Sub
或者,您也可以使用以下代码:
Sub ShowDifference()
Dim cmdButton As Button
For Each cmdButton In ActiveSheet.Buttons
If cmdButton.Name = "cmdShowDif" Then
If cmdButton.Caption = "Show Difference" Then
cmdButton.Caption = "Show All"
Else
cmdButton.Caption = "Show Difference"
End If
Else
Debug.Print cmdButton.Name & " is not the one... moving to next button..."
End If
Next cmdButton
End Sub
如果您有任何疑问,请告诉我。
转到“开发人员”选项卡,然后单击“设计模式”。 现在选择您的CommandButton。 按钮的名称将出现在名称栏中-在编辑栏的左侧。 更改此行
Set cmdButton = ActiveSheet.Shapes("cmdShowDif")
至
Set cmdButton = ActiveSheet.OLEObjects("cmdShowDif").Object
但这会使用正确的名称而不是cmdShowDif
(或将名称框中的名称更改为cmdShowDif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.