0

如何将此宏分配给跨多个工作表的按钮;IE。无论按钮的名称是“按钮 1”还是“按钮 2”?

Public SelectionChange_Enabled As Boolean
Sub Button1_Click()
If ActiveSheet.Shapes("Button 1").TextFrame.Characters.Text = "Disable Events" Then
    ActiveSheet.Shapes("Button 1").TextFrame.Characters.Text = "Enable Events"
    SelectionChange_Enabled = False
Else
    ActiveSheet.Shapes("Button 1").TextFrame.Characters.Text = "Disable Events"
    SelectionChange_Enabled = True
End If
End Sub
4

1 回答 1

3

您可以使用Application.Caller来确定什么叫做例程。你可以像这样使用它

只更新被点击的按钮

Sub AllButtons_Click()
    With ActiveSheet.Shapes(Application.Caller)
        If .TextFrame.Characters.Text = "Disable Events" Then
            .TextFrame.Characters.Text = "Enable Events"
            SelectionChange_Enabled = False
        Else
            .TextFrame.Characters.Text = "Disable Events"
            SelectionChange_Enabled = True
        End If
    End With
End Sub

并将所有按钮分配给AllButtons_Click

更新所有工作表中的按钮(使用 this Sub

Sub AllButtons_Click2()
    Dim shp As Button
    Dim sh As Worksheet

    SelectionChange_Enabled = Not SelectionChange_Enabled
    For Each sh In ThisWorkbook.Worksheets
        For Each shp In sh.Buttons
            If shp.OnAction = ThisWorkbook.Name & "!AllButtons_Click2" Then
                If SelectionChange_Enabled Then
                    shp.Caption = "Disable Events"
                Else
                    shp.Caption = "Enable Events"
                End If
            End If
        Next
    Next

End Sub
于 2013-05-12T01:22:41.270 回答