How to return a value from a function?

  • Thread starter Thread starter Sing
  • Start date Start date
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.
 
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
 
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
 
Back
Top