0

In the following code, why does the block format If Then Else execute without error, but the single line version generates a syntax error?

Test = False

Sub SubA
  MsgBox "A"
End Sub

Sub SubB
  MsgBox "B"
End Sub

If Test Then
  SubA
Else 
  SubB
End If

If Test Then SubA Else SubB 'Syntax error
LesFerch
  • 1,540
  • 2
  • 5
  • 21
  • 1
    I've created this question to document this behavior since I could not find any duplicate here or anywhere else on the Internet. I already know that this error only occurs with a Sub that takes no parameters and the workaround is to change `SubA` to `Call SubA`. I plan to post that as answer to my own question, but if anyone has any further insight, that would be welcome. – LesFerch Feb 20 '23 at 22:52
  • 1
    Huh. `If Test Then SubA: Else SubB` works as well, something to do with [new lines](https://stackoverflow.com/a/67097180/4935162), I believe. – Yarin_007 Feb 20 '23 at 23:20
  • @Yarin_007 Cool. That's another good workaround. Thanks. – LesFerch Feb 20 '23 at 23:25
  • From the [official documentation](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/5h27x7e9(v=vs.84)#single-line-syntax) - "When you use the single-line syntax, you can execute multiple statements as the result of an If...Then decision, but they must all be on the same line and separated by colons." – user692942 Feb 21 '23 at 07:06
  • @user692942 I've seen that documentation and, of course, colons are required for multiple statements after the `Then` but there's no indication that a colon is required when there's a single statement after `Then` followed by `Else`. And, as far as I can determine, the only case where the Syntax error occurs is what I've described. The fact that the Syntax error does not occur by adding `Call` would indicate that this is just a weird parsing exception. – LesFerch Feb 21 '23 at 15:46
  • @LesFerch `Call` act's as another way of identifying the start of a statement so the colon isn’t required. It's not a bug, it’s just how the runtime works. It’s the same for variable assignments as they to denote the start of a statement. Related [How to do a single line If statement in VBScript for Classic-ASP?](https://stackoverflow.com/q/20353072/692942) – user692942 Feb 21 '23 at 18:35
  • @user692942 I guess the presence of parenthesis also denotes a statement, since `If Test Then SubA(1) Else SubB` will also work (when SubA is changed to require a parameter). – LesFerch Feb 21 '23 at 19:56
  • @LesFerch actually in the case of parenthesis it’s a bit misleading as it’s [not what you think](https://stackoverflow.com/a/13622374/692942). – user692942 Feb 21 '23 at 20:04
  • 1
    @user692942 Yup. I should have also mentioned that `If Test Then SubA 1 Else SubB` also runs without error. I suppose the bottom line is that `SubA` by itself, without a line end, is ambiguous in regards to being a statement. – LesFerch Feb 21 '23 at 20:45
  • @LesFerch exactly that. – user692942 Feb 22 '23 at 08:49

0 Answers0