Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA中的IF语句_Vba_Excel - Fatal编程技术网

VBA中的IF语句

VBA中的IF语句,vba,excel,Vba,Excel,我使用的是给出错误消息的if语句 End if无阻塞if 我试着把End if放在sub之前,然后放在if语句之后 If IDtype = "UK" Then sqluserfix = " & UserId & " If IDtype = "NE" Then sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')" End If 我还尝试在每个if语句的末

我使用的是给出错误消息的
if
语句
End if无阻塞if

我试着把
End if
放在
sub
之前,然后放在
if
语句之后

If IDtype = "UK" Then sqluserfix = " & UserId & "

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

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

If IDtype = "UK" Then sqluserfix = " & UserId & "

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

End If
有什么想法吗


谢谢。

如果使用多行格式,则只能使用
End If

If something Then
    'do something
End 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

顺便说一下,这些语句也可以如下所示:

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


演示如何以正确的方式执行此操作。

VBA
If
语句可以像这样内联执行:

If IDtype = "UK" Then sqluserfix = " & UserId & "
或者可以在多行上执行,如下所示:

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

您的问题是同时混合了这两种格式,选择一种格式,您的问题就会得到解决。

在您的情况下,您使用的是所谓的单行语句

这是有效的,不需要使用
End If
语句,因为它是一个单行程序

因为您有一个
End If
语句,这就是发生错误的原因,因为您试图结束一个尚未启动的
If
语句(两行一行)

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


你可以用另外两种方法做同样的事情

(一)

(二)


然而,在您的情况下,一个更合适的决定似乎是像这样使用
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

只有两种选择吗?只是很惊讶没有人建议选择案例

Select Case IDtype
    Case "UK"
        sqluserfix = " & UserId & "
    Case "NE"
        sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End Select

+如果我是外行,这会让我明白:)这也说明了错误的原因。+1如果我是外行,这对我来说真的很清楚:)这也解决了错误的原因。-1虽然我感谢你努力分享知识,但目前形式的答案似乎并没有解决这个问题(比如问:“我的车为什么不能启动?”,答:“你可以坐公共汽车”)。将此作为对问题的评论或对已接受答案的评论发布似乎更合适。如果您发现格式化选项太有限,您可以将知识作为一个新的(自我回答的)问题(例如,“VBA中条件代码执行的选项是什么?”)分享,并在此处添加注释以链接到该问题。或链接到现有答案,如以下:
If IDtype = "UK" Then
    sqluserfix = " & UserId & "
ElseIf IDtype = "NE" Then
    sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End If
Select Case IDtype
    Case "UK"
        sqluserfix = " & UserId & "
    Case "NE"
        sqluserfix = "(select USER_ID from USER_PARAMS where USER_ID2='" & UserId & "')"
End Select