If then else Excel VBA - 需要“End If”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37564915/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:26:44  来源:igfitidea点击:

If then Else Excel VBA - "End If" needed?

excelvbaif-statementudf

提问by LaH

so I m writing a UDF in Excel vba of the kind:

所以我在 Excel vba 中编写了一个 UDF:

function ... if ... then ... else for i... to ... equations ... next i * end function

function ... if ... then ... else for i... to ... equations ... next i * end function

Now I noticed, that the function seems to work just fine, however I was wondering whether I shouldnt be using an "end if" at the position of (*) in the code? If I do, I receive an error msg stating there was no if corresponding IF to that "end if" block, though!? So in general, isn't there a Need for an "end if" in if then else constructions ? Thanks in advance!

现在我注意到,该函数似乎工作得很好,但是我想知道是否不应该在代码中的 (*) 位置使用“end if”?如果我这样做了,我会收到一条错误消息,说明没有对应于“end if”块的 IF,但是!?所以一般来说,在 if then else 结构中是否需要“结束 if”?提前致谢!

/edit: 'if ... then ... else' is a one liner. However the else block does contain multiple lines and in particular a loop...

/edit: 'if ... then ... else' 是一行。但是 else 块确实包含多行,特别是一个循环......

回答by MutjayLee

If you put IF THEN ELSE in one line, then the if condition ends on that line and next line will be executed no matter what.

如果将 IF THEN ELSE 放在一行中,则 if 条件在该行结束,无论如何都将执行下一行。

For example,

例如,

If true then x =1 else x = 2
y = 1

this case

这个案例

if true, x will be 1 and y will be 1

如果为真,x 将是 1,y 将是 1

if false, x will be 2 and y will be 1

如果为 false,则 x 为 2,y 为 1



if true then
    x = 1
else
    x = 2
    y = 1
end if

this case,

这个案例,

if true, x will be 1 and y will be empty

如果为真,x 将为 1,y 将为空

if false, x will be 2 and y will be 1

如果为 false,则 x 为 2,y 为 1



In addition, if you do

另外,如果你这样做

If true then x = 1 else 
x = 2

x will always be 2 no matter true or false

无论真假,x 永远是 2

回答by Na Nonthasen

If you don't want END IF, but still want IF, ELSE IF, ELSE, just write all in single line.
Use ":" for multiple actions.

如果您不想要 END IF,但仍想要 IF、ELSE IF、ELSE,只需将所有内容都写在一行中。
使用“:”进行多项操作。

Sub test(Optional x As Long)

    If x < 0 Then MsgBox "x<0" Else If x = 0 Then MsgBox "x=0": MsgBox "I told ya x=0" Else MsgBox "x>0": MsgBox "I told ya x>0"

End Sub

回答by Duncan

Yes, you need the End If statement or you will get an error.

是的,您需要 End If 语句,否则您将收到错误消息。

It is only when your If statement is a one-liner that you don't need it, like:

只有当您的 If 语句是单行语句时才不需要它,例如:

If 1 = 1# Then result = "true"

but:

但:

If 1 = 1# Then
    result = "true"

doesn't work unless it is closed out with End If like this:

除非像这样用 End If 关闭,否则不起作用:

If 1 = 1# Then
    result = "true"
End If

Can't see exactly how you structured your function, but if it works without End If, then I guess it is a valid one-liner If statement or you have On Error Resume Next somewhere in your code.

无法确切地看到您如何构建您的函数,但如果它在没有 End If 的情况下工作,那么我猜它是一个有效的单行 If 语句,或者您的代码中某处有 On Error Resume Next 。