Bishop Minter
Bishop Minter

Reputation: 107

Why won't this VBA If True Then statement compile?

Here's the code snippet

Dim POSelectedNum As CimObjectVariable
Set POSelectedNum = CimGetScriptOwner().GetVariable("POSelectedNum")

If (StrComp("POSelectedNum" = "POSelectedNum", vbTextCompare) = 0) = True
Then POSelectedNum = ""
End IF

I keep getting this error:

Error 168 in line 44: Encountered: end of line

Expecting: , Then

And there's a red bar at the end of True. I searched for Error 168 on VBA but came up dry. Any help?

Upvotes: 1

Views: 90

Answers (2)

Mathieu Guindon
Mathieu Guindon

Reputation: 71187

It won't compile because the statement is grammatically incorrect.

There are two valid syntaxes for an If statement.

Inline:

If {bool-expression} Then {statement}

Block:

If {bool-expression} Then
    {statements}
End If

Anything else is a compile error.


If you really want the Then part on another line together with the statement that follows, you could use the inline syntax with line continuations (underscores):

If {bool-expression} _
Then {statement}

Note the absence of an End If token: while there are 2 "physical" lines of code, as far as VBA is concerned this is one single "logical" line of code, and parses as a correct inline syntax.

Upvotes: 2

Preston
Preston

Reputation: 8187

I imagine that you mean this:

Dim POSelectedNum As CimObjectVariable
Set POSelectedNum = CimGetScriptOwner().GetVariable("POSelectedNum")

If StrComp(POSelectedNum, "POSelectedNum", vbTextCompare) = 0 Then
    POSelectedNum = ""
End IF

Upvotes: 2

Related Questions