2013-05-12 42 views
0

如何将此宏分配给多张纸上的按钮;即。不管按钮的名称是“按钮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 

回答

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

要更新按钮进行的​​跨所有工作表(即使用这种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 
+0

谢谢。我怎么能更新所有按钮的文本,以便当我点击一个按钮时,所有其他按钮注册相同的更改? – user2319146 2013-05-12 01:36:08

+0

嗯...通过“所有按钮”我想我的意思是所有的按钮,AllButtons_Click分配给他们:) – user2319146 2013-05-12 02:02:37

+0

哈,小心你想要什么。查看更新 – 2013-05-12 02:09:33

相关问题