Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting IF statement in multiple lines in VBScript

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.

like image 948
Saikat Avatar asked Feb 03 '14 10:02

Saikat


People also ask

How to continue next line in VBscript?

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.

How do you do a line break in VBscript?

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.

How do you add a line in VBscript?

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).

When writing codes you might have long statements which can be broken by?

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.


1 Answers

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.

like image 121
Siddharth Rout Avatar answered Oct 12 '22 06:10

Siddharth Rout