1

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
3
  • It wouldn't allow me to post my line right before the elseifmeb = "platinum" line. I begun my if before that. Commented Apr 21, 2017 at 7:41
  • VB.NET is not the same as VBA. Commented Apr 21, 2017 at 7:43
  • 1
    Without the line before the ElseIf, we probably can't help answer your question. (But if it says something like If something Then something then you can't have an ElseIf after a "single-line" If. Just as you can't have an End If after your later "single-line" If statements such as If st >= min Then cb = st * d Else cb = 0. Commented Apr 21, 2017 at 7:44

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.