Doug Coats
Doug Coats

Reputation: 7107

Multiple If conditions in Excel

I clearly have a syntax issue and would like to know where am I magically missing the point. Got tired of trying to find the answer so I decided to ask.

Code just wants to check if X is not any of three specific variables, and if not then P = 3, other P will equal 1 or 2 based on a combo box result.

I tried using Or statements with this and no luck.

 If X <> 15 Then
     P = "3"
 Else
     If X <> 18 Then
         P = "3"
     End If
         Else
             If X <> 20 Then
                 P = "3"
             End If
 ElseIf ComboBox <> "Other Condition" Then
     P = 1
     Else: P = 2
 End If

Upvotes: 2

Views: 80800

Answers (3)

Matt Cremeens
Matt Cremeens

Reputation: 5151

I prefer

If x <> 15 and x <> 18 and x <> 20 then
    P = 3
ElseIf ComboBox <> "Other Condition" then
    P = 1
Else
    P = 2
End If

Your version has and Else followed by an End If, which is incorrect syntax. This version is more succinct and easier to read, IMHO.

Upvotes: 3

Denise Skidmore
Denise Skidmore

Reputation: 2416

If X <> 2 And X <> 3 And X <> 4 Then
    P = 3
ElseIf ComboBox <> "OtherCondition" Then
    P = 1
Else
    P = 2
End If

Upvotes: 11

Scott Craner
Scott Craner

Reputation: 152505

Try this:

If x = 15 Or x = 18 Or x = 20 Then
    If ComboBox = "Other Condition" Then
        P = 2
    Else
        P = 1
    End If
Else
    P = 3
End If

Upvotes: 2

Related Questions