我花了一些时间才发现不能将 Application.Evaluate() 函数与还包含 Application.Evaluate() 的用户定义函数一起使用。请参阅下面的代码示例。
Public Sub main()
Dim func$: func = "some_function()"
Debug.Print Application.Evaluate(func)
End Sub
Public Function some_function() As Variant
some_function = Application.Evaluate("0.0003")
End Function
这似乎是一个 VBA 错误。不过,我很欣赏如何处理这个问题,同时避免在 UDF 代码中使用 Application.Evaluate() 。谢谢!
您应该使用
Application.Run
。只需在指定函数名称时删除括号即可
Public Sub main()
Const func = "some_function"
Debug.Print Application.Run(func)
End Sub
或者,当用作 UDF 时
Public function myUDF()
Const func = "some_function"
myUDF = Application.Run(func)
End Sub