尝试这个
替换Call pro
为Application.Run pro
例子
Private Sub test_Click()
Dim i As String
Dim pro As String
i = 1
pro = "sale_call" + i
'~~> This will run sale_call1
Application.Run pro
i = 2
pro = "sale_call" + i
'~~> This will run sale_call2
Application.Run pro
End Sub
Sub sale_call1()
MsgBox "Hello"
End Sub
Sub sale_call2()
MsgBox "goodbye"
End Sub
跟进
如果您的代码不在模块中,而是在用户表单或工作表代码区域中,那么Application.Run
直到那时才会起作用,sale_call1
或者sale_call2
没有放在模块中。如果您不想将它们移动到模块中,则必须使用CallByName
. 查看 Excel 对此功能的内置帮助。这是一个假设代码在Userform1
Private Sub CommandButton1_Click()
Dim i As String
Dim pro As String
i = 1
pro = "sale_call" + i
'~~> This will run sale_call1
CallByName UserForm1, pro, VbMethod
i = 2
pro = "sale_call" + i
'~~> This will run sale_call2
CallByName UserForm1, pro, VbMethod
End Sub
Sub sale_call1()
MsgBox "Hello"
End Sub
Sub sale_call2()
MsgBox "goodbye"
End Sub