Share via

Is polymorphism possible in vba function declarations?

Anonymous
2010-09-28T22:53:00+00:00

Hello super awesome forum contributors,

Does anyone know how to make the following work in Excel?

I'd like to have a function

function param_overload(var_overloaded as Range) as string

   param_overload ="var_overloaded_as_param"

end function

function param_overload(var_overloaded as string) as string

    param_overload = "var_overloaded as string"

end function

I get an ambiguous name definition error in VBA.

any ideas are welcome.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-09-30T18:05:28+00:00

Dave,  thanks for the suggestions.  On the track of the variant parameter, how do you type check a variant object to know what it actually is.

The VBA help documentation has not been so helpful (as you said above :) )

You can use the TypeName or VarType functions, or you can test its TypeOf like this...

If TypeOf YouVariant Is "TextBox" Then

depending on how you want to structure your code.


NOTE: Please mark the message or messages (yes, you can mark more than one) that answer your question as the "Answer" so that others will know your question has been resolved.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2010-09-28T23:32:42+00:00

Pass it a variant and have your code check to see what it is.

And while you're reading VBA's help <hehe>, remember to look at paramarray under

the help for the Function Statement.

SantaMonicaMike wrote:

i've seen some vendors provide functions where they use the same name in the function but you have the option of using a parameter that is a string or a parameter that is a range.  How is this possible then?

--

Dave Peterson

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-09-28T23:24:45+00:00

    Although I think that this is usually a bad thing to do, you could put the

    functions in separate modules in the same project.

    If you're calling the UDF from a formula in a cell in a worksheet, just prepend

    the module name onto the function call:

    =Module1.param_overload(a1)

    or

    =Module1.param_overload(a1)

    Same kind of thing if you're calling the function in code from a different

    module.  (If the calling code is in the same module, then the function in that

    module will be used.)

    Option Explicit

    Sub aa()

        dim myStr as string

        mystr = param_overload(activesheet.range("a1")

    End Sub

    Again, I wouldn't do this.  You may be vigilant today, but maybe not so much

    tomorrow -- or the poor soul that will have to update your code.

    SantaMonicaMike wrote:

    Hello super awesome forum contributors,

    Does anyone know how to make the following work in Excel?

    I'd like to have a function

    function param_overload(var_overloaded as Range) as string

       param_overload ="var_overloaded_as_param"

    end function

    function param_overload(var_overloaded as string) as string

        param_overload = "var_overloaded as string"

    end function

    I get an ambiguous name definition error in VBA.

    any ideas are welcome.

    --

    Dave Peterson

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-09-28T23:19:22+00:00

    i've seen some vendors provide functions where they use the same name in the function but you have the option of using a parameter that is a string or a parameter that is a range.  How is this possible then?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-09-28T23:02:18+00:00

    That is because in Excel VBAA you can have at most one function named param_overload


    gsnu201005

    Was this answer helpful?

    0 comments No comments