Why does this code compile when pasted in but fail otherwise?
With such a long line of code, it's hard to spot where the compile error creeps in, but there is a subtle difference that appears to be the VBE applying an autocorrect to the line as, or more likely after, it is parsed.
This is the original line - As pasted from the clipboard
The line appears like this until you move the cursor to another line.Notice the colon, in bold, between the Loop
and Else
keywords statement:
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency:
Loop: Else
Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
This is the line after you move the cursor to another line:
Notice the colon has been auto-removed by VBE. It seems like the VBE parser recognizes the statement, and then "optimizes" the "redundant" colon away.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency:
Loop Else
Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
If you add back the colon to a line that is in the invalid syntax state, then the autocorrect kicks in again, but your line returns to being valid, but fragile code.
So, it seems that VBE parses the line, identifies an optimization (the redundant colon), and then applies the fix, but VBE doesn't realize the optimized line has syntax problems.
But why does the line end up being fragile? There are lots of distracting and irrelevant keywords in the line, so let's reduce it drastically:
Do While..Loop
If we minimize the complexity of the line, to isolate the problem, we can simplify the line to:
If True Then Do While True: Beep: Loop: Else
Which, again, VBE autocorrects to a fragile line:If True Then Do While True: Beep: Loop Else
But we can go even further and reduce the line to an illogically short line:If True Then Do: Loop: Else
And VBE, once again, dutifully removes the colon to produce this line: (DO NOT EXECUTE THIS LINE OR YOU WILL HANG EXCEL)If True Then Do: Loop Else
While..Wend
Repeating the line, but swapping out the
Do While Loop
, for the olderWhile Wend
syntax:If True Then While True: Beep: Wend: Else
Again, VBE optimizes away the colon, to give:If True Then While True: Beep: Wend Else
But now the line is not fragile anymore!
So, the While..Wend is the older construct, and the Do..Loop construct is newer (and more flexible), but it seems the VBE parser (and syntax optimizer) struggle with the Do..Loop construct.
Key point: Don't use Do..Loop
in Single-Line If
statements that include an Else
statement.
I would adventure to answer that the Resume Next
is a key instruction here, since anything else invalid would skip to the next instruction.
Colons separate the commands as if they were new lines.
It is otherwise very inriguing indeed.