1

I have a combobox in Word that is supposed to populate the termShorthand text field based on the selection from the termWritten array. I am receiving the Block If without End If compile error even though I have it after my If statements.

Private Sub termWritten_DropButtonClick()
    termWritten.List = Array("first", "second", "third", "final")
End Sub

Private Sub termWritten_Change()
    If termWritten.Value = "first" Then
        termShorthand.Value = "three (3)"
    Else
        If termWritten.Value = "second" Then
            termShorthand.Value = "two (2)"
        Else
            If termWritten.Value = "third" Then
                termShorthand.Value = "one (1)"
            Else
                If termWritten.Value = "final" Then
                    termShorthand.Value = "no"
                End If
End Sub

3 Answers3

3

You need an End If statement for each If statement, like this:

Private Sub termWritten_Change()
    If termWritten.Value = "first" Then
        termShorthand.Value = "three (3)"
    Else
        If termWritten.Value = "second" Then
            termShorthand.Value = "two (2)"
        Else
            If termWritten.Value = "third" Then
                termShorthand.Value = "one (1)"
            Else
                If termWritten.Value = "final" Then
                    termShorthand.Value = "no"
                End If 'final
            End If 'third
        End If 'second
    End If 'first
End Sub

You can learn more about the If...Then...Else statement on Microsoft Docs.

2

@twisty impersonator's correct regarding the syntax for if/then/else, but your code would be simpler to follow and update if you used Select Case instead:

Private Sub termWritten_Change()

Select Case termWritten.Value
   Case Is = "first
      termShorthand.Value = "three (3)"
   Case Is = "second"
      termShorthand.Value = "two (2)"
   ' and so on, adding another Case Is = "xyz" for each value
   ' you want to test for.  At the end, it's usually a good idea to
   ' include
   Case Else
     ' This runs if no other conditions are met
     ' Use it to set an error code, supply a default value, etc.
End Select

End Sub

And following twisty's example, I'm adding a link to MS' documentation for Select Case:

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/select-case-statement

1

While Select Case is probably better it's worth mentioning that you could have used ElseIf and then only one End If would be required

Private Sub termWritten_DropButtonClick()

   termWritten.List = Array("first", "second", "third", "final")

End Sub

Private Sub termWritten_Change()

   If termWritten.Value = "first" Then

       termShorthand.Value = "three (3)"

   ElseIf termWritten.Value = "second" Then

       termShorthand.Value = "two (2)"

   ElseIf termWritten.Value = "third" Then

       termShorthand.Value = "one (1)"

   ElseIf termWritten.Value = "final" Then

       termShorthand.Value = "no"

   End If

End Sub