VBA中的IF语句 [英] IF statements in VBA

查看:166
本文介绍了VBA中的IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果如果发送错误消息
,我正在使用结束如果没有阻止,如果



Ive尝试将结束,如果就在并且刚刚在如果语句之后。



我还尝试放置2 end if 语句在每个 IF 语句的末尾。

 如果IDtype =UKthen sqluserfix =& UserId&

如果IDtype =NE然后sqluserfix =(从USER_PARAMS选择USER_ID其中USER_ID2 ='& UserId &')

如果

任何想法? p>

谢谢。

解决方案

在你的情况下,你正在使用所谓的一行语句

  If(true)then MsgBoxNo End If required! 

这样做并不需要 End If 语句,因为它是一个



因为你 有一个结束如果语句是错误发生的原因,因为您尝试结束一个如果语句尚未启动( 2 one-liners



在C#中,例如; 分号用作行分隔符。在VB / VBA中,您可以使用返回键来分隔行。






您可以使用其他两种方式做同样的事情



1)

  If(true)then _ 
MsgBoxNo End If required!

2)

 code> If(true)then 
MsgBoxEnd If required !!!
结束如果






然而在你的情况下似乎更合适的决定将是使用的组合,如果 ElseIf 像这样

 如果IDtype =UK然后
sqluserfix =& UserId&
ElseIf IDtype =NE然后
sqluserfix =(从USER_PARAMS中选择USER_ID,其中USER_ID2 ='& UserId&')
End If


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.

解决方案

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

这篇关于VBA中的IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆