Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

excel - IF statements in VBA

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your case you are using what is called a one line statement

If (true) then MsgBox "No End If required!"

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 an If 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)

If (true) then _ 
    MsgBox "No End If required!"

2)

If (true) then
    MsgBox "End If required!!!"
End If

However in your case it seems that a more suitable decision would be to use a combination of If and ElseIf like this

If IDtype = "UK" Then
    sqluserfix = " & UserId & "
ElseIf IDtype = "NE" Then
    sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End If

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...