我已经获得了一些代码来构建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
等辅助方法?
答案 0 :(得分:3)
这是一个例子
Sub Sample()
Dim Thing
Thing = "Sid"
Application.OnKey "^{s}", "'RunWith""" & Thing & """'"
End Sub
Sub RunWith(Thing)
MsgBox Thing
End Sub