ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Question: Macro overloading, passing variable number of arguments (https://www.excelbanter.com/excel-programming/271028-question-macro-overloading-passing-variable-number-arguments.html)

Frederik Romanov

Question: Macro overloading, passing variable number of arguments
 
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.

Tom Ogilvy

Question: Macro overloading, passing variable number of arguments
 
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.



--
Regards,
Tom Ogilvy


"Frederik Romanov" wrote in message
om...
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.





All times are GMT +1. The time now is 01:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com