How to return a value from a function?

S

Sing

Dear Excel Gurus,

In C, one can do the following;

int func()
{
....
if (x == true)
return y;
....
}

How can I do the same thing in Excel VBA? I wish to return a value and exit
without running the function to the end.

Thanks for your advice in advance.
 
R

RB Smissaert

Function GetValue() as Long

Dim i as Long

For i = 1 to 10
If i = 5 Then
GetValue = i
Exit Function
End If
Next i

End Function


So, the answer is Exit Function.


RBS
 
D

Dana DeLouis

General ideas...

Function Func()
Dim x As Boolean
Dim y As Double

x = True
y = 3.14

If x Then
Func = y
Exit Function
End If

'Other stuff
Func = 1.23
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top