gaussblurinc
gaussblurinc

Reputation: 3682

VBA: Unexpected Behavior In IF-statement.

I am using VBA for Microsoft Excel 2007. My code is as follows:

 Sub First()

 End Sub

 Function Two() As Boolean

 End Function

 Sub Sun()
     If (Two()) Then
         First()
     End If
 End Sub

What is wrong with this code? Why does it not compile?

Can I not use subs in IF statements? Is it a magic of VBA? How can or should I resolve this issue?

Upvotes: 1

Views: 296

Answers (2)

qxn
qxn

Reputation: 17574

Try removing the parenthesis from the call to First.

Sub First()

End Sub

Function Two() As Boolean

End Function

Sub Sun()
    If (Two()) Then
        First
    End If
End Sub

Upvotes: 3

Nick Spreitzer
Nick Spreitzer

Reputation: 10588

This compiles:

Sub First()

End Sub

Function Two() As Boolean

End Function

Sub Sun()
    If (Two()) Then
        First
    End If
End Sub

You need to remove the parentheses from your First call.

Upvotes: 0

Related Questions