停止VBA从调用目标函数两次评估 [英] Stop VBA Evaluate from calling target function twice

查看:117
本文介绍了停止VBA从调用目标函数两次评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBA的Evaluate()函数只执行一次,我遇到麻烦;它似乎总是运行两次。例如,考虑下面的微不足道的例子。如果我们运行RunEval()子例程,它将调用EvalTest()函数两次。这可以通过在即时窗口中打印的两个不同的随机数来看出。如果我们使用Evaluate而不是一个函数调用另一个子程序,行为将是一样的。有人可以解释一下我可以如何评估执行目标函数一次而不是两次?谢谢。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆