I was wondering if in VBScript I can break a If
statement in multiple lines. Like:
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.
Instead, use the underscore ( _ ) to indicate that a statement is continued on the next line. In the revised version of the script, a blank space and an underscore indicate that the statement that was started on line 1 is continued on line 2.
What is the syntax for VBscript? The vbCrLf equates to doing a Chr(13) + Chr(10) combination. You could also use vbNewLine, which is replaced with whatever value the platform considers a newline. On Windows, the result of vbCrLf and vbNewLine should be the same.
If you want to force a new line in a message box, you can include one of the following: The Visual Basic for Applications constant for a carriage return and line feed, vbCrLf. The character codes for a carriage return and line feed, Chr(13) & Chr(10).
To break a single statement into multiple lines The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.
Yes you can break IF statement in multiple lines in vbscript. Here is a very basic example
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With