评估()在VBA [英] Evaluate() in VBA
问题描述
您好,欢迎来到评估())神秘
MSDN Office开发者参考(2013)文档说:
使用方括号(例如,[A1:C5])与使用字符串参数的Evaluate方法调用
相同。
所以,我运行了一个非常简单的代码来查看 Microsoft的Evaluate()方法的文档。
毫不奇怪,我得到了一个奇怪的,虽然一致的结果。
注意: 执行每个立即窗口中的4个命令
CTRL + G 。查看每个电话的区别。请注意,内置的 错误 ,其中 显示每个MsgBox两次 。只要记住这一点,不要困惑...
将此代码粘贴到模块中
Private Sub SleepESub()
Application.Wait Now + TimeValue(0:00:20)
MsgBoxw8'd
End Sub
然后在立即窗口中执行这4个命令(每次1个)
?评估(SleepESub())
? [SleepESub()]
? [SleepESub]
? SleepESub
第一个2立即执行代码;对我来说,他们已经评估了代码。第三个(根据文档)应该是 评估
,但它的行为方式不同>在模块的主体中。立即窗口提供错误2023
但是,从模块的正文中执行相同的调用,就像您正在调用子程序一样。它等待 20秒
喜欢,如果它是一个普通的调用SleepESub()
这是4号呼叫。
任何人都可以解释我在这里缺少什么?行号3不适合评估
通话?或者是否评估对子本身的调用(如果有意义)
更新:
我认为有些人误会了我在这里评估 - 不要担心这是一个高级话题,我不是书作家,你不介意读者。 (原谅我... )
要想得到一个更好的主意,您可以比较直接窗口与模块的正文的结果。尝试以下代码:
'在模块的正文中分别运行每个调用
'
'来自立即窗口的以前的调用
Sub ModuleBody()
Evaluate(SleepESub())
'[SleepESub()]
'[ SleepESub]
'SleepESub
End Sub
在我看来,执行代码的不同方式有什么不同之处在于它运行的线程 - UI线程或后台线程以及解析器。 评估
执行函数的处理方式与显式定义的函数不同,从立即窗口调用的函数也会略有不同。
In:
Sub ModuleBody()
Evaluate(SleepESub())
[SleepESub()]
[SleepESub]
SleepESub
End Sub
评估(SleepESub())
和 [SleepESub()]
似乎在期待一个公式,
根据解析器如何处理该过程,每个命令可以在单个线程中依次执行,导致从 Application.Wait
或 Application.Wait
可能被认为仅在UI线程上有效,并且在后台线程上运行时跳过。
这可以由以下代码确认,由?[SleepESub( )]
或?在立即窗口中评估(SleepESub())
私有声明PtrSafe Sub sapiSleep Libkernel32别名睡眠(ByVal dwMilliseconds As Long)
Private Sub SleepESub()
'Application.Wait Now + TimeValue (0:00:05)
sapiSleep 5000
MsgBoxw8'd
End Sub
当使用 sapiSleep 5000
API调用时,等待发生(两次! - 所提到的错误),但是当使用 Application.Wait Now + TimeValue(0:00:05)
时,不会发生延迟。
Hi and welcome to the Evaluate() mystery
The MSDN Office Developer Reference (2013) Documentation says:
Using square brackets (for example, "[A1:C5]") is identical to calling the Evaluate method with a string argument.
So, I have ran a very simple code to see how accurate the Microsoft's Documentation of the Evaluate() method is.
Not surprisingly, I am getting a strange albeit consistent result.
note: execute each of the 4 commands in the Immediate Window
CTRL+G. See the difference in each of the calls. Notice the built-in bug which shows each MsgBox twice. Just keep that in mind and do not get confused...
Stick this code in a module
Private Sub SleepESub()
Application.Wait Now + TimeValue("0:00:20")
MsgBox "w8'd "
End Sub
then execute these 4 commands ( 1 at a time ) in the Immediate Window
? Evaluate ("SleepESub()")
? [SleepESub()]
? [SleepESub]
? SleepESub
The first 2 execute the code right away; means to me they have evaluated the code. The third one (according to the documentation) should be Evaluating
but it doesn't act the same way as it does in a module's body. The Immediate Window is giving an Error 2023
however the same call from within a module's body executes it as if you were calling a sub.It waits the 20 seconds
like if it was a normal Call SleepESub()
which is the number 4 call.
Can anyone explain what I am missing here? Is the line number 3 not a proper Evaluation
call? or does it evaluate the call to sub itself (if that makes sense)
Update:
I think some people are misunderstanding what I am evaluating here - don't worry it is an advanced topic and I am not a book writer and you are not mind readers. (forgive me...)
To get a better idea you can compare results from the immediate window vs. module's body. Try this code:
' Run each of the calls separately
' in a module's body and compare it with
' the previous calls from the Immediate Window
Sub ModuleBody()
Evaluate ("SleepESub()")
'[SleepESub()]
'[SleepESub]
'SleepESub
End Sub
It would appear to me that what differs in the different ways of executing the code would be the thread that it runs on - the UI thread or a background thread, and the parser. Evaluate
executed functions would be handled differently to explicitly defined functions, and functions called from the Immediate window would be handled slightly differently also.
In:
Sub ModuleBody()
Evaluate ("SleepESub()")
[SleepESub()]
[SleepESub]
SleepESub
End Sub
Evaluate ("SleepESub()")
and [SleepESub()]
appear to be expecting a formula, and Private Sub SleepESub()
is not being executed at all.
Depending on how the parser handles the procedure, each command may be executed in sequence in a single thread, resulting in the delay from the Application.Wait
, or the Application.Wait
may be considered to be valid only on the UI thread, and skipped when run on a background thread.
This can be confirmed by the following code, executed by ?[SleepESub()]
or ?Evaluate("SleepESub()")
in the Immediate window:
Private Declare PtrSafe Sub sapiSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Private Sub SleepESub()
'Application.Wait Now + TimeValue("0:00:05")
sapiSleep 5000
MsgBox "w8'd "
End Sub
When using the sapiSleep 5000
API call, the wait occurs (twice! - that bug that was mentioned), but when using Application.Wait Now + TimeValue("0:00:05")
, no delay occurs.
这篇关于评估()在VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!