Mac版Excel VBA中如何通过变量间接调用过程?
Mac版Excel VBA通过变量调用过程的实现方案
你遇到的问题是VBA不支持直接用Call加字符串变量的方式调用过程,因为Call要求后面必须是明确的过程/函数名,无法自动解析字符串变量中的内容,所以会报“Expected Sub, Function, or Property”错误。
针对Mac版Excel VBA,推荐使用Application.Run方法来实现通过变量调用过程的需求,这是跨平台兼容的方案,具体修改代码如下:
Sub Test() Dim ProcName As String ProcName = "test1" ' 使用Application.Run执行变量存储的过程名 Application.Run ProcName End Sub Sub test1() MsgBox "test1 过程已被成功调用" ' 测试用提示,可根据需求修改 End Sub
补充说明:
- 如果目标过程位于指定模块中,需要在过程名前加上模块名,格式为
模块名.过程名,例如:ProcName = "Module1.test1" Application.Run ProcName - 如果过程需要传递参数,直接在
Application.Run后追加参数即可,示例:Sub TestWithParam() Dim ProcName As String ProcName = "testWithArgs" Application.Run ProcName, "参数1", 123 End Sub Sub testWithArgs(str As String, num As Integer) MsgBox "收到参数:" & str & "," & num End Sub
Mac版VBA确实存在部分与PC端不一致的特性,但Application.Run是官方支持的跨平台方法,能稳定实现动态调用过程的需求。
内容的提问来源于stack exchange,提问作者Steve
相关产品推荐
相关产品推荐

