单击所有按钮的VBA宏

时间:2017-07-19 16:29:41

标签: excel vba excel-vba

我试图通过获取所有按钮名称并评估对这些函数的调用来创建一个单击任何文档中所有按钮的宏。

代码:

Private Sub CommandButton1_Click()
MsgBox "b"
End Sub

Private Sub CommandButton2_Click()
MsgBox "a"
End Sub

Sub test_macro()
For Each S In Worksheets(1).OLEObjects
    Evaluate ("Call " + S.Name + "_Click")
Next
End Sub

这可能是什么问题?是否有另一种方法可以做到这一点?

1 个答案:

答案 0 :(得分:2)

看看是否有效。

For Each OLEObject In Worksheets(1).OLEObjects
    If TypeName(OLEObject.Object) = "CommandButton" Then OLEObject.Object = True
Next

Here are some additional documentations on OLEObject.