Question: Macro overloading, passing variable number of arguments

  • Thread starter Frederik Romanov
  • Start date
F

Frederik Romanov

Excel-97 (SR-2)

In general I would like to write macros that can be overloaded, for
example Hyperlink() has an optional displaytext field.

HyperLink( Filename)
HyperLink( Filename, DisplayText)

Please could someone post a short, useful macro that does such a
thing, rather than give a description of how to do so.

I have searched the online help for "overloading" and "arguments" but
nothing useful is shown there.

TIA,
Fred.
 
T

Tom Ogilvy

See the help for Functions

You are just talking about optional arguments.

Function MyFunc(MyStr As String, Optional MyArg1 As _ Integer = 5, _
Optional MyArg2 = "Dolly")
Dim RetVal

' The function can be invoked as follows:
RetVal = MyFunc("Hello", 2, "World") ' All 3 arguments supplied.
RetVal = MyFunc("Test", , 5) ' Second argument omitted.
' Arguments one and three using named-arguments.
RetVal = MyFunc(MyStr:="Hello ", MyArg1:=7)


The arglist argument has the following syntax and parts:

[Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [=
defaultvalue]

Part Description
Optional Optional. Indicates that an argument is not required. If
used, all subsequent arguments in arglist must also be optional and declared
using the Optional keyword. Optional can't be used for any argument if
ParamArray is used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top