Calc Guide

6) Save, close, and reopen the Calc document. This time, the
NumberFive() function works.
Listing 4. Change the name of NumberFive to
NumberFive_Implementation.
Function NumberFive()
If NOT BasicLibraries.isLibraryLoaded("AuthorsCalcMacros") Then
BasicLibraries.LoadLibrary("AuthorsCalcMacros")
End If
NumberFive = NumberFive_Implementation()
End Function
Passing arguments to a macro
To illustrate a function that accepts arguments, we will write a macro
that calculates the sum of its arguments that are positive it will
ignore arguments that are less than zero (see Listing 5).
Listing 5. PositiveSum calculates the sum of the positive arguments.
Function PositiveSum(Optional x)
Dim TheSum As Double
Dim iRow As Integer
Dim iCol As Integer
TheSum = 0.0
If NOT IsMissing(x) Then
If NOT IsArray(x) Then
If x > 0 Then TheSum = x
Else
For iRow = LBound(x, 1) To UBound(x, 1)
For iCol = LBound(x, 2) To UBound(x, 2)
If x(iRow, iCol) > 0 Then TheSum = TheSum + x(iRow, iCol)
Next
Next
End If
End If
PositiveSum = TheSum
End Function
The macro in Listing 5 demonstrates a couple of important techniques:
1) The argument x is optional. If the argument is not optional and it
is called without an argument, OOo prints a warning message
every time the macro is called. If Calc calls the function many
times, then the error is displayed many times.
2) IsMissing checks that an argument was passed before the
argument is used.
Chapter 12 Calc Macros 351