I am using an if
statement which is giving the the error message
End if without block if
Ive tried placing the End if
just before the sub
and just after the if
statement.
I also tried placing 2 end if
statements at the end of each IF
statement.
If IDtype = "UK" Then sqluserfix = " & UserId & "
If IDtype = "NE" Then sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End If
Any ideas?
Thanks.
Are there only 2 options? Just surprised no one has suggested
Select Case
You can only use
End If
if you use multi-line formatting:In this case use
ElseIf
:By the Way this Statements can also look like this:
Formating Code in VBA shows you how to do this in a right way.
In your case you are using what is called a one line statement
This works and doesn't require an
End If
statement because it's a one-liner.Because you do have an
End If
statement that's why the error occurs, because you trying to end anIf
statement which has not been started ( 2 one-liners ).In C# you for example a
;
semi-colon acts as a line separator. In VB/VBA you use the Return key to separate lines.You can do exactly the same thing two other ways
1)
2)
However in your case it seems that a more suitable decision would be to use a combination of
If
andElseIf
like thisVBA
If
statements can be done inline like so:Or be done on multiple lines like so:
Your problem is that you are mixing both at the same time, choose one format and your problem will be fixed.