Search code examples
vbaif-statementbasic

If and ElseIf Statement in VBA


I'm working on a form in VBA for a word document of mine. I'm learning as I go along. I have a combo box where you can select an item from the list. Basically, I want the document to print out 4 copies if the item "Homestretch" is selected and 3 copies for everything else that is selected or typed into the combo box.

I have the 4 copies printing out perfectly when I select Homestretch, but if I select anything else it won't print. Also note, this is under the command button click function and I only want this to execute if the ckbPrint checkbox is checked. Here's the code below. Thanks.

If Me.ckbPrint.Value = True Then
If cbxCarrier.Value = "Homestretch" Then
     ActiveDocument.PrintOut copies:=4
ElseIf cbxCarrier.Value <> "Homestretch" Then
ElseIf Me.ckbPrint.Value = True Then
     ActiveDocument.PrintOut copies:=3
End If
End If 

Solution

  • Your code is applying too many tests. You can simplify this to the following:

    Dim copies As Long
    
    If Me.ckbPrint Then
        If cbxCarrier.Value = "Homestretch" Then
            copies = 4
        Else
            copies = 3
        End If
        ActiveDocument.PrintOut copies:=copies
    End If