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
?
for the last two.Run("SleepESub")
andRun("SleepESub()")
are similar butEvaluate("SleepESub")
doesn't work. What's going on?! – Antihistamine? Evaluate("SleepESub")
returnsError 2029
; same for? [SleepESub]
. I would conclude that 2010 is "clean" (matches the documentation) – Rialto? [SleepESub]
(called from the Immediate Window) does return aError 2029
, however If you make the same call within a module's body it does work, although is not Evaluating it just executes as if you were calling a sub. I am starting to wonder if the square brackets are just being ignored in this case as the compiler thinks[SleepESub] = SleepyESub
. On the other hand, if the Immediate Window is giving an error how come the same call from a modules body works? – BaileybailiePublic
, then[SleepESub]
works from the Immediate Window. Same result as from module's body (no apparent evaluation). – RialtoExpected function or variable.
error appears even in case when its not made private in Excel 2010. I dont think it's the visibility issue. Also, I am not sure why I am getting an error but it works for you :o – Baileybailie