Lupp wrote: ↑Fri Oct 31, 2025 5:07 pm
However, when writing user code for Calc I often missed the Excel/VBA feature ParamArray.
I don't use Excel or VBA, but my understanding is that the ParamArray feature allows one to pass a variable number of parameters to a function as the last argument. For a Calc user-defined-function, I don't need the ParamArray feature since I can just pass variable-length arrays from Calc to a StarBasic function. Perhaps the ParamArray feature is needed because Excel and/or VBA are deficient and can't pass arrays like Calc and StarBasic can.
The array from Calc would often be a cell range, but it could also be an array constant. With a StarBasic function, an array can be passed as
any argument, it doesn't have to be the last one. All that's needed is for the user-defined-function to be expecting the array(s). The function can use UBOUND to determine how many items are being passed.
This attachment shows an example of passing 3, 4, or 5 parameters in the second argument of a 3-argument function. This UDF ignores the first and third arguments to demonstrate that it isn't necessary to pass the array at the end of the parameter list.
=ZAVERAGE("Foo";{1;2;0;4;5};"Bar")
This is the function, showing how to handle an array. To keep it simple, focusing on the current topic, I have omitted the extra code to check p2 for a scalar instead of an array, or to check for strings instead of numbers in the array. Those tests can be done with VARTYPE.
Option Explicit
Function ZAVERAGE(p1 As Variant,p2 As Variant,p3 As Variant) As Variant
Rem p1 and p3 are ignored; Returns avarage of non-zero values in p2
Rem If the second argument is an array, it will be a 2-dimensional array
Rem Calc always passes arrays dimensioned (1 To RowCount,1 To ColumnCount)
Dim s As Double : Dim c As Long ' Sum and count
Dim i As Long : Dim j As Long ' Loop indices
s = 0 : c = 0 ' Initialize sum and count
For i = 1 To UBOUND(p2,1) ' Outer loop
For j = 1 To UBOUND(p2,2) ' Inner loop
s = s + p2(i,j) ' Add the value in the row
If p2(i,j) <> 0 Then c = c + 1 ' Count non-zero numbers
Next j ' End inner loop
Next i ' End outer loop
If c = 0 Then ZAVERAGE = "#DIV/0!" : Exit Function ' No non-zero numbers
ZAVERAGE = s/c ' Return average
End Function
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.6, iMac Intel. The locale for any menus or Calc formulas in my posts is English (USA).