VBA Excel 2010:帮助在工作表范围内使用Application.Run

所以我试图在Excel2010的工作表macros中使用Application.Run调用任意函数。 在位于“Sheet1”中的以下代码中,为什么z2返回正确答案,而z3返回一个Empty变体?

Option Explicit Function myfun11(x) As Double myfun11 = x + 10 Debug.Print "I WORK!" End Function Sub test1230() Dim x, output1, output2 Dim str1 x = 1 str1 = "Sheet1.myfun11" output1 = Sheet1.myfun11(x) output2 = Application.Run(str1, x) End Sub 

在代码片段中,Debug.print被调用两次,这意味着Application.Run成功运行该函数。 但是,它无法检索output2 (但成功获取output1)。 我在这样的“模块”范围中再次testing了代码(代码位于模块“Module1”中),并且Application.Run成功检索了output1和output2:

 Option Explicit Function myfun11(x) As Double myfun11 = x + 10 Debug.Print "I WORK!" End Function Sub test1230() Dim x, output1, output2 Dim str1 x = 1 str1 = "Module1.myfun11" output1 = Module1.myfun11(x) output2 = Application.Run(str1, x) End Sub 

谁能解释为什么会发生这种情况? 这是一个VB错误还是有这个问题的原因? 也有人有这个问题的解决方法?

好吧,我终于find了一个解决方法。 我不知道为什么这个工作(或者更重要的是,为什么Application.Run不起作用),但它确实如此。 看起来Application.Run似乎并没有特别好的工作。

相反,我可以使用:

 output3 = CallByName(Sheet1, "myfun11", VbMethod, x)