The information in this article applies to:
SUMMARY
Using Microsoft Excel, you can create user-defined functions that can be
called from a worksheet. These functions accept arguments in their
function calls that can be of any type. Often, you need to be able to pass
a varying amount of arguments to the function, depending on the situation.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.asp Method 1: The Paramarray KeywordUsing the Paramarray keyword, the function can accept any number of arguments, which are all stored in the array that follows the Paramarray keyword. For example, as shown in the following Sub procedure, Testit, five arguments are sent to the function, each one being placed into the nums array with a separate index number. Next, the upper bound of the nums array is found using the Microsoft Visual Basic for Applications function, UBOUND, which returns the largest available subscript for an array that is passed to it. In the following example, because Paramarray is filled with five arguments, n = 5, Paramarray is always an array of Variants; it cannot be declared as any other type.
Method 2: The Optional KeywordThe Optional keyword should be used in situations where there is an upper bound on the number of arguments that are going to be passed. For example, if the maximum number of arguments for a function is not going to exceed three or four, you can use the Optional keyword in conjunction with the ISMISSING function. The ISMISSING function returns either TRUE or FALSE, depending on what arguments were passed into the function. Arguments that use the Optional keyword must always be declared as Variant.
Method 3: Using Public VariablesIt is also possible, but not recommended, to use variables with a Public scope as the "arguments" of a user-defined function. Using Public variables as inputs for a function is not the same as passing arguments; the function call does not include the arguments themselves; they are accessible to all functions in the workbook. This method will work, but because all of the procedures in the workbook have access to these variables, it is possible that they will have unwanted values.
Each of these methods will work in all cases, but each one will work more
efficiently than the others in certain situations. For example, using an
array in Visual Basic for Applications will always be more efficient than
using individual variables. This must be weighed against the extra code
that must be put into the macro to cycle through the Paramarray. Also,
using the Paramarray to contain your arguments tends to lessen the
readability of your code because it is impossible to tell what arguments
have been passed without cycling through the array.
In terms of memory storage space, Method 3 uses the least amount of storage. The Variant data type used in Visual Basic for Applications will accept any of the supported data types and automatically make the conversion to the correct data type internally. However, because they must accommodate many different storage sizes, Variant data types use 16 bytes of storage space in memory. Because both the Optional keyword and the Paramarray keyword must use Variants as their data type, they will allocate much more space in memory than simply not using arguments and declaring public variables of the correct type. For example, if a function must have as input four or five integers, if you declare four or five Public variables and assign the numbers to them, the total storage space required would only be 8 to 10 bytes as opposed to 64 to 80 bytes using the Optional or Paramarray keywords. Using Public variables as shown in Method 3 is not recommended because they are not passed to the function; they are only referenced. Also, using Public variables as inputs to a function will not enter them into the Function Wizard. The user-defined function will have no arguments listed in the Function Wizard. REFERENCESFor more information about custom functions, please see the following article in the Microsoft Knowledge Base: Q112321 XL5: Macro Fails If Optional Argument Uses Integer Data Type Additional query words: 5.00a 5.00c 7.00a 8.00 XL97 XL7 XL5
Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |