将macros分配给多个工作表中的button

我怎样才能将这个macros分配给多个工作表中的button; 即。 不pipebutton名称是“button1”还是“button2”?

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 

您可以使用Application.Caller来确定所谓的例程。 你可以像这样使用它

只更新被点击的button

 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 

并将所有button分配给AllButtons_Click

要更新所有工作表中的button(使用此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