vba 在 VBScript 中将 IF 语句拆分为多行

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

Splitting IF statement in multiple lines in VBScript

excel-vbavbscriptvbaexcel

提问by Saikat

I was wondering if in VBScript I can break a Ifstatement in multiple lines. Like:

我想知道在 VBScript 中是否可以将If语句分成多行。喜欢:

If (UCase(Trim(objSheet.Cells(i, a).Value)) = "YES") Or _
(UCase(Trim(objSheet.Cells(i, b).Value)) = "NO") Then

' Do something

End If

I tried this and got a syntax error as Ifexpects a Thenin the same line :(

我想这一点,得到了一个语法错误,因为If需要一个Then在同一行:(

采纳答案by Siddharth Rout

Yes you can break IF statement in multiple lines in vbscript. Here is a very basic example

是的,您可以在 vbscript 的多行中中断 IF 语句。这是一个非常基本的例子

If 1 = 1 Or _
2 = 2 Then

wscript.echo "See, It Works :)"

End If

or

或者

If (UCase(Trim("1")) = "1") Or _
(UCase(Trim("2")) = "2") Then

wscript.echo "See, It Works :)"

End If

The error is somewhere else. Check your workbook objects and their values. Also check the values of i, aand b.

错误在其他地方。检查您的工作簿对象及其值。还要检查ia和的值b

回答by marcw

Yes, line breaks in if statements are supported.

是的,支持 if 语句中的换行符。

I ran the following code both in Excel/VBA and as a single vbscript and it worked without throwing an error.

我在 Excel/VBA 和单个 vbscript 中运行了以下代码,并且它没有抛出错误。

Dim aStr
aStr = "yeah"

If (UCase(Trim(aStr)) = "YES") Or _
   (UCase(Trim(aStr)) = "NO") Then
  MsgBox "yes/no"
Else
  MsgBox "no action"
End If

Or is it a problem with objSheet? Did you try to set a variable for UCase(Trim(objSheet.Cells(i, a).Value)? Did it show the expected Value?

还是objSheet有问题?您是否尝试为 设置变量UCase(Trim(objSheet.Cells(i, a).Value)?它是否显示了预期值?