Splitting IF statement in multiple lines in VBScript

I was wondering if in VBScript I can split the If statement into several 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 received a syntax error, as If expects Then on the same line :(

+7
source share
2 answers

Yes, you can split the IF statement on multiple lines in vbscript. Here is a very simple 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 elsewhere. Check the objects of your book and their meanings. Also check the values โ€‹โ€‹of i , a and b .

+7
source

Yes, line breaks in if operations are supported.

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

 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? Have you tried setting a variable for UCase(Trim(objSheet.Cells(i, a).Value) ? Did the expected value UCase(Trim(objSheet.Cells(i, a).Value) ?

+3
source

All Articles