停止VBA从调用目标函数两次评估 [英] Stop VBA Evaluate from calling target function twice
问题描述
Sub RunEval()
评估EvalTest()
End Sub
公共函数EvalTest()
Debug.Print Rnd()
结束函数
这个错误似乎只发生在UDF上,而不是内置的功能。
您可以通过添加一个表达式绕过它:
Sub RunEval()
ActiveSheet.Evaluate0 + EvalTest()
End Sub
但是有还有一些其他的限制,评估,这里记录
http://www.decisionmodels.com /calcsecretsh.htm
I am having trouble getting VBA's Evaluate() function to only execute once; it seems to always run twice. For instance, consider the trivial example below. If we run the RunEval() subroutine, it will call the EvalTest() function twice. This can be seen by the two different random numbers that get printed in the immediate window. The behavior would be the same if we were calling another subroutine with Evaluate instead of a function. Can someone explain how I can get Evaluate to execute the target function once instead of twice? Thank you.
Sub RunEval()
Evaluate "EvalTest()"
End Sub
Public Function EvalTest()
Debug.Print Rnd()
End Function
This bug only seems to happen with UDFs, not with built-in functions.
You can bypass it by adding an expression:
Sub RunEval()
ActiveSheet.Evaluate "0+EvalTest()"
End Sub
But there are also a number of other limitations with Evaluate, documented here http://www.decisionmodels.com/calcsecretsh.htm
这篇关于停止VBA从调用目标函数两次评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!