VBA 中的 IF 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20541024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IF statements in VBA
提问by user3013325
I am using an if
statement which is giving the the error message
End if without block if
我正在使用一个if
给出错误消息的语句
End if without block if
Ive tried placing the End if
just before the sub
and just after the if
statement.
我试过将 放在语句End if
之前sub
和之后if
。
I also tried placing 2 end if
statements at the end of each IF
statement.
我还尝试end if
在每个IF
语句的末尾放置 2 个语句。
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.
谢谢。
回答by
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.
这有效并且不需要End If
声明,因为它是单行的。
Because you dohave 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).
因为您确实有一个End If
语句,这就是发生错误的原因,因为您试图结束If
尚未启动的语句(2 个单行)。
In C# you for example a ;
semi-colon acts as a line separator. In VB/VBA you use the Returnkey to separate lines.
例如,在 C# 中,;
分号充当行分隔符。在 VB/VBA 中,您使用Return键来分隔行。
You can do exactly the same thing two other ways
您可以通过另外两种方式做完全相同的事情
1)
1)
If (true) then _
MsgBox "No End If required!"
2)
2)
If (true) then
MsgBox "End If required!!!"
End If
However in your case it seems that a more suitable decisionwould be to use a combination of If
and ElseIf
like this
但是,在您的情况下,似乎更合适的决定是使用If
和ElseIf
这样的组合
If IDtype = "UK" Then
sqluserfix = " & UserId & "
ElseIf IDtype = "NE" Then
sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End If
回答by laylarenee
VBA If
statements can be done inline like so:
VBAIf
语句可以像这样内联完成:
If IDtype = "UK" Then sqluserfix = " & UserId & "
Or be done on multiple lines like so:
或者像这样在多行上完成:
If IDtype = "NE" Then
sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End If
Your problem is that you are mixing both at the same time, choose one format and your problem will be fixed.
您的问题是您同时混合了两者,选择一种格式,您的问题将得到解决。
回答by Bathsheba
You can only use End If
if you use multi-line formatting:
只有End If
在使用多行格式时才能使用:
If something Then
'do something
End If
回答by engineersmnky
Are there only 2 options? Just surprised no one has suggested Select Case
只有2个选项吗?只是很惊讶没有人建议Select Case
Select Case IDtype
Case "UK"
sqluserfix = " & UserId & "
Case "NE"
sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End Select
回答by Smartis
In this case use ElseIf
:
在这种情况下使用ElseIf
:
If IDtype = "UK" Then
sqluserfix = " & UserId & "
ElseIf IDtype = "NE" Then
sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End If
By the Way this Statements can also look like this:
顺便说一下,这个语句也可以是这样的:
If IDtype = "UK" Then
sqluserfix = " & UserId & "
End If
If IDtype = "NE" Then
sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End If
Formating Code in VBAshows you how to do this in a right way.