Search code examples
libreoffice-calclibreoffice-basic

LibreOffice Calc macro function accessing cell range


I have the function from Function accessing cell range (repeated here):

public function CHECKBZRANGE(vCellRangeValues as variant) as integer

    dim i as integer
    dim vCellValue as variant

    for each vCellValue in vCellRangeValues
        msgbox vCellValue
        i = i + 1
    next

    CHECKBZRANGE = i
end function

which used to work in LibreOffice <=6.x.y, e.g. as =CHECKBZRANGE(A6:C9). Now in LibreOffice 7.0.0.3 I get this error message

Inadmissible value or data type.
Data type mismatch.
[OK]

in the line

for each vCellValue in vCellRangeValues

I searched for this, but could not find an answer. Is this:

  1. A new feature of LibreOffice VBA?
  2. A regression?

How do I access the values of range in a macro function?


Solution

  • Since vCellRangeValues ​​is not a range of cells, but an array of values ​​of these cells, you should use two nested loops to display each value - over rows and columns:

    Function CHECKBZRANGE(vCellRangeValues As Variant) As Integer 
    Dim iRow As Long, iColumn As Long 
        For iRow = LBound(vCellRangeValues) To UBound(vCellRangeValues)
            For iColumn = LBound(vCellRangeValues,2) To UBound(vCellRangeValues,2)
                msgbox "Cell (" & iRow & "," & iColumn & ") = " & vCellRangeValues(iRow, iColumn )
            Next iColumn
        Next iRow
        
        CHECKBZRANGE = (UBound(vCellRangeValues)-LBound(vCellRangeValues) +1) * _
                                        (UBound(vCellRangeValues,2)-LBound(vCellRangeValues,2) +1)
    End Function