在VBScript中以多行拆分IF语句

时间:2022-08-19 07:28:56

I was wondering if in VBScript I can break a IF statement 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 If expects a Then in the same line :(

我尝试了这个,并得到一个语法错误,因为如果期望一个然后在同一行:(

2 个解决方案

#1


6  

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, a and b.

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

#2


2  

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)?它是否显示了预期值?

#1


6  

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, a and b.

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

#2


2  

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)?它是否显示了预期值?