VBA包含OnAction中子例程的参数

时间:2014-07-17 08:50:38

标签: excel vba excel-vba

我已经获得了一些代码来构建Excel VBA中的菜单,如下所示:

For Each Thing In Array("Foo", "Bar", "Baz")
    With .Controls.Add(Type:=msoControlButton)
         .Caption = "Run with " & Thing
         .FaceId = 2934
         .OnAction = "'" & ThisWorkbook.Name & "'!" & "RunWith" & Thing
    End With
Next Thing

Sub RunWithFoo()
  RunWith "Foo"
End Sub

Sub RunWithBar()
  RunWith "Bar"
End Sub

Sub RunWithBaz()
  RunWith "Baz"
End Sub

Sub RunWith(Thing) ...etc

有没有办法直接调用RunWith而无需通过RunWithFoo等辅助方法?

1 个答案:

答案 0 :(得分:3)

这是一个例子

Sub Sample()
    Dim Thing
    Thing = "Sid"
    Application.OnKey "^{s}", "'RunWith""" & Thing & """'"
End Sub

Sub RunWith(Thing)
    MsgBox Thing
End Sub