Microsoft Office Tutorials and References
In Depth Information
Some Useful Worksheet Functions
The SPELLDOLLARS function is too lengthy to list here, but you can view the complete
listing in worksheet function.xlsm on the companion CD-ROM.
A multifunctional function
This example describes a technique that may be helpful in some situations: making a single
worksheet function act like multiple functions. For example, the following VBA listing is for a custom
function called StatFunction . It takes two arguments: the range ( rng ) and the operation ( op ).
Depending on the value of op , the function returns a value computed using any of the following
worksheet functions: AVERAGE, COUNT, MAX, MEDIAN, MIN, MODE, STDEV, SUM, or VAR.
For example, you can use this function in your worksheet as follows:
The result of the formula depends on the contents of cell A24, which should be a string such as
Average , Count , Max , and so on. You can adapt this technique for other types of functions.
Function StatFunction(rng, op)
Select Case UCase(op)
StatFunction = WorksheetFunction.Sum(rng)
StatFunction = WorksheetFunction.Average(rng)
StatFunction = WorksheetFunction.Median(rng)
StatFunction = WorksheetFunction.Mode(rng)
StatFunction = WorksheetFunction.Count(rng)
StatFunction = WorksheetFunction.Max(rng)
StatFunction = WorksheetFunction.Min(rng)
StatFunction = WorksheetFunction.Var(rng)
StatFunction = WorksheetFunction.StDev(rng)
StatFunction = CVErr(xlErrNA)