jjjjjjjjjj
jjjjjjjjjj

Reputation: 11

VBA Else without if error

I keep getting the " Else without if" error in VBA when I clearly do not have that issue. Does anyone know how to fix it? It takes me to the elseif statement that begins with elseif memb= "platinum" Below is my code:

ElseIf memb = "Platinum" Then d = 0.01

ElseIf memb = "Black" Then d = 0.03

End If

If st >= min Then cb = st * d Else cb = 0
End If
If cb >= thresh Then MsgBox ("cb is greater than thresh")
End If

tac = st + cb

Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac

Upvotes: 1

Views: 4899

Answers (2)

Rowen Chumacera
Rowen Chumacera

Reputation: 529

I'm going to assume your first If statement goes something like this:

If memb = "Gold" Then d = 0.005

ElseIf memb = "Platinum" Then d = 0.01

ElseIf memb = "Black" Then d = 0.03

End If

If some processing is performed on the same line as the Then keyword, VBA treats it as a single, non-nested If statement. This means that anything after that will be treated as a new statement and not related to prior If statement.

What you can do is put the processing statement(s) on the next line after each If-Then and ElseIf-Then statements.

Example,

If memb = "Gold" Then
  d = 0.005
ElseIf memb = "Platinum" Then
  d = 0.01
ElseIf memb = "Black" Then
  d = 0.03
End If

With this in mind, you may want to fix the succeeding If-Then-Else statements in your code. The End If part becomes meaningless if your If-Then-Else is in a single line.

Upvotes: 2

vv01f
vv01f

Reputation: 372

Your code seems to have syntax error and error message tells you that. Or you did not post all code?

Have a look on MS documentation: https://msdn.microsoft.com/de-de/library/752y8abs.aspx

Do you really stick to the syntax?

Without even having MS OFfice this should (be better readable and) work:

If memb = "Platinum" Then
  d = 0.01
ElseIf memb = "Black" Then
  d = 0.03
End If

If st >= min Then
  cb = st * d
Else
  cb = 0
End If

If cb >= thresh Then
  MsgBox ("cb is greater than thresh")
End If

tac = st + cb

Range("B5").Value = st
Range("B7").Value = cb
Range("B9").Value = tac

Upvotes: 0

Related Questions