ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to return a value from a function? (https://www.excelbanter.com/excel-programming/397961-how-return-value-function.html)

Sing

How to return a value from a function?
 
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.



RB Smissaert

How to return a value from a function?
 
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



"Sing" wrote in message
...
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.




Dana DeLouis

How to return a value from a function?
 
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

--
HTH :)
Dana DeLouis
Windows XP & Excel 2007


"Sing" wrote in message
...
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.





Sing

How to return a value from a function?
 
Thank you for your replies. You have been most helpful!!

"Sing" wrote in message
...
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.






All times are GMT +1. The time now is 08:24 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com