Home » excel » excel – VBA: Unexpected Behavior In IF-statement.

excel – VBA: Unexpected Behavior In IF-statement.

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

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.