Overloading functions in VBA

  • Can you overload functions in VBA? I thought you could but I'm getting the ambiguous name error. I have two functions:

    Function FindTableSheet(strTableName as String) as String

    ' code here

    End Function

    Function FindTableSheet (strTableName as String, ByRef strShName as String) as boolean

    ' code here

    End function

    I'm typing this in on a tablet so I may have missed typed something there. Both functions search active workbook sheets for the given strTableName. The first function returns the sheet name if it finds a table by the name in strTableName. The second one does the same but returns true if if find the table and false if it doesn't. Set strShName to the Sheet.name on success or vbNullChar on failure.

    Maybe I have my languages mixed up but I thought this was legal. Thanks. Chunk.

    Edited once, last by royUK ().

  • Welcome to Ozgrid Forum. Please read the Forum Rules before continuing to learn how to use the Forum correctly. I have added Code Tags this time.

    You cannot have two Functions or Subs with the same name.

  • Hi FE,

    No you cannot have two functions with the same name, that is causing the ambiguous name error. Just combine the code in the one function or call a second function with a different name from the first.

    That is my understanding anyway.


  • As others have said, no overloading in vba. It has optional parameters instead (with default values if needed)

    sub blah(byval s as string, optional byval i as long)
    sub blah(byval s as string, optional byval i as long = 10)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!