Home > other >  Can single line VBA If Then be followed by single line ElseIf Then?
Can single line VBA If Then be followed by single line ElseIf Then?

Time:01-12

Trying to follow a single line If Then with single line ElseIf Then, but the ElseIf Then's are not executed.

Does not work. No error. Just doesn't do the ElseIf's.

If WD = "Sat" Or WD = "Sun" Then State = "Z"
ElseIf HH >= 16 Then State = "A"
ElseIf HH >= 13 Then State = "B"
ElseIf HH >= 5 Then State = "C"
ElseIf HH >= 4 Then State = "D"
ElseIf HH >= 0 Then State = "E"

Works as expected:

If WD = "Sat" Or WD = "Sun" Then
    State = "Z"
ElseIf HH >= 16 Then State = "A"
ElseIf HH >= 13 Then State = "B"
ElseIf HH >= 5 Then State = "C"
ElseIf HH >= 4 Then State = "D"
ElseIf HH >= 0 Then State = "E"
End If

Is it possible to follow a single line If Then with single line ElseIf Then's?

Microsoft Visual Basic for Applications 7.1
Microsoft Office Professional Plus 2013
Excel 2013

CodePudding user response:

No. The inline syntax is designed for short conditions and quick Then and (maybe) quick little Else statements. If the conditions are non-trivial, you need to use block syntax.

Inline syntax is a statement on its own; in VBA the statement terminator is a newline, so an ElseIf token cannot begin a line of legal code if the previous line didn't register as a block-syntax conditional.

Not sure what you mean with "no error" about the first snippet... it's a compile error... which prevents the code from running at all:

'Else without If' compile-time error message

That said, there are still other ways to express these conditions:

State = "Z"
If WD = "Sat" Or WD = "Sun" Then Exit Sub
Select Case HH
    Case Is >= 16
        State = "A"
    Case Is >= 13
        State = "B"
    Case Is >= 5
        State = "C"
    Case Is >= 4
        State = "D"
    Case Is >= 0
        State = "E"
End Select

CodePudding user response:

While Mathieu's answer is technically correct - this is not possible with if statements - something equivalent can actually be achieved by using line continuation syntax with Select Case in a way he didn't showcase.
Select Case can also evaluate any condition just like if statements by using Select Case True.

The following showcases this "one line ElseIf":

Sub Demo()
    Dim State As String, Wd As String, HH As Long
    HH = 10
    Wd = "Do" '"Sat"
    Select Case True:
        Case Wd = "Sat" Or Wd = "Sun": State = "Z"
        Case HH >= 16: State = "A"
        Case HH >= 13: State = "B"
        Case HH >= 5: State = "C"
        Case HH >= 4: State = "D"
        Case HH >= 0: State = "E"
    End Select
    Debug.Print State
End Sub

Also, by using Select Case you can actually go a step further and put the entire statement into a single line (up to 1024 characters):

Sub Demo()
    Dim State As String, Wd As String, HH As Long
    HH = 10
    Wd = "Do" '"Sat"
    Select Case True: Case Wd = "Sat" Or Wd = "Sun": State = "Z": Case HH >= 16: State = "A": Case HH >= 13: State = "B": Case HH >= 5: State = "C": Case HH >= 4: State = "D": Case HH >= 0: State = "E": End Select
    Debug.Print State
End Sub
  • Related