Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function optional variables (Excel 2003 VBA)

    I know this is possible in java, but I'm not sure about VBA:
    Is it possible to have both:
    private function IDoSomething(strValue1 as String, strValue2 as String, strValue3 as String)
    private function IDoSomething(strValue1 as String, strValue2 as String)

    and have VBA call the appropriate function based on the number of arguments?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Function optional variables (Excel 2003 VBA)

    You can do this:

    Private Function IDoSomething(strValue1 As String, strValue2 As String, Optional strValue3 As String)

    or

    Private Function IDoSomething(strValue1 As String, strValue2 As String, Optional strValue3 As String = "defaultvalue")

    Type Optional in the VB editor and press F1 to get help.

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Function optional variables (Excel 2003 VBA)

    Hi,
    You can't overload functions in VB6 or VBA (though you can in .Net) but there are ways around it in some circumstances. You can also use Parameter Arrays in addition to Hans' workaround.
    HTH.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Function optional variables (Excel 2003 VBA)

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •