簡體   English   中英

Excel VBA更改Excel命令按鈕上的標題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM