ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Returning by Function (https://www.excelbanter.com/excel-programming/284336-returning-function.html)

Syed Zeeshan Haider[_4_]

Returning by Function
 
Hello Experts,
I have Excel 97 Pro on Win98SE.

For a certain complex calculation, I have written a specific function in
VBA. This function returns a date. For a certain situation, I want this
function to return a string. Can a function return two different data types
for two different conditions?

Thank you,
--
Syed Zeeshan Haider.
http://szh.20m.com/


-----------------------------------
Allah says to Mankind:
"Then which of the favours of your Lord will ye deny?"



Bill Manville

Returning by Function
 
Syed Zeeshan Haider wrote:
Can a function return two different data types
for two different conditions?


Yes.
Declare it As Variant

Function GiveMeA(What As String) As Variant
Select Case What
Case "Date": GiveMeA = Date()
Case "String": GiveMeA = "Hello"
End Select
End Function

=GIVEMEA("Date") ' with cell formatted as date
=GIVEMEA("String") ' cell format can still be date.


Bill Manville
MVP - Microsoft Excel, Oxford, England
No email replies please - reply in newsgroup


Syed Zeeshan Haider[_4_]

Returning by Function
 
"Bill Manville" wrote in message ...
Syed Zeeshan Haider wrote:
Can a function return two different data types
for two different conditions?


Yes.
Declare it As Variant

Function GiveMeA(What As String) As Variant
Select Case What
Case "Date": GiveMeA = Date()
Case "String": GiveMeA = "Hello"
End Select
End Function

=GIVEMEA("Date") ' with cell formatted as date
=GIVEMEA("String") ' cell format can still be date.


Thank you! It works perfectly.

Due to some unknown (in fact, unremembered) reasons my tests gave me
negative results bundled with some quite annoying error messages.

New tests have proved it to be working perfectly.

Thanks again,
--
Syed Zeeshan Haider.
http://szh.20m.com/


-----------------------------------
Allah says to Mankind:
"Then which of the favours of your Lord will ye deny?"






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

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