Returning by Function

  • Thread starter Thread starter Syed Zeeshan Haider
  • Start date Start date
S

Syed Zeeshan Haider

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 said:
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
 
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,
 
Back
Top