1

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?

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks! and if First(arg as Variant) i must write First arg, yes?
@loldop -- I think so. VBA has some strange argument requirements. If the sub/function returns a value, and that value is placed in a variable, then parenthesis are required. If no value is returned, then you have to omit the parenthesis.
Here's a nice write up of parentheses by Rick Rothstein. dailydoseofexcel.com/archives/2012/05/01/…
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.