Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用字符串值调用VBA sub_Vba_Excel - Fatal编程技术网

使用字符串值调用VBA sub

使用字符串值调用VBA sub,vba,excel,Vba,Excel,这是我的测试代码 Sub dotask() Dim qusub As String qusub = Worksheets("Task List").Range("C2").Value MsgBox qusub Application.Run qusub End Sub Sub msg1() MsgBox "sub msg1" End Sub Sub msg2() MsgBox "sub msg2" End Sub Sub msg

这是我的测试代码

Sub dotask()

    Dim qusub As String
    qusub = Worksheets("Task List").Range("C2").Value

    MsgBox qusub
    Application.Run qusub

End Sub

Sub msg1()

    MsgBox "sub msg1"

End Sub

Sub msg2()

    MsgBox "sub msg2"

End Sub

Sub msg3()

    MsgBox "sub msg3"

End Sub

Sub msg4()

    MsgBox "sub msg4"

End Sub
所有这些都包含在一个标准模块中。我已经根据我在那里找到的内容(即使用Application.Run)读写了我的代码。任务列表工作表的C2单元格目前包含“msg3”。当我执行sub“dotask”时,我首先得到一个消息框,上面写着我想要的“msg3”,但随后我得到以下错误消息:

运行时错误“1004”:

无法运行宏“msg3”。该宏可能在此工作簿中不可用,或者可能已禁用所有宏


我正在使用Excel 2010,文件是
.xlsm
-我应该怎么做才能让我的代码按我所希望的方式执行?

使用GetRef,您可以将引用提供给sub。 例如,见我的问题

编辑:根据评论中的建议,这里是这个问题的部分解决方案

sub one(para)
  WScript.Echo para & " from one"
end sub

sub two(para)
  WScript.Echo para & " from two"
end sub

sub main(subname, para)
  Dim f : Set f = GetRef(subname)
  f para
end sub

main "one", "test" '=>test from one

就在这里查过了。msg1似乎是一个保留字。。。将它更改为其他模块,它工作正常=)

Hmm,此代码是否包含在模块中?是的,所有这些都包含在单个模块中,并且它是此文件中唯一的模块请参见前面的问题:
Application.Run“Module1.msg1”
也工作正常。奇怪的是,我甚至不能从宏对话框中运行msg1宏。这个对话框在低质量Post队列中弹出。我在说skip,但我认为这需要更多的解释