0

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?

sphh
  • 35
  • 6

1 Answers1

1

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 
JohnSUN
  • 2,268
  • 2
  • 5
  • 12
  • By the way, the fact that the function takes an array of values ​​as a parameter, and not a range of cells, is stated in the answer to which you refer. – JohnSUN Sep 14 '20 at 07:20
  • Thanks. That's a working workaround (and maybe a cleaner, but more elaborate, way). Nevertheless it does _not_ answer my question, why the `for each vCellValue in vCellRangeValues` does not work anymore. I use it quite extensively in my macros, which I now have to rewrite. A regression? A changed design decision? @JohnSUN, can you shed some light on it? – sphh Sep 15 '20 at 11:12
  • Do you still have `Option VBASupport 1` in the second line of the module? It's [HERE](https://help.libreoffice.org/6.4/en-US/text/sbasic/shared/03103350.html?&DbPAR=BASIC&System=WIN) – JohnSUN Sep 15 '20 at 11:27
  • Adding `Option VBASupport 1` did it. Now the macro from my question works again for the latest LibreOffice. To recap: For LibreOffice <7 I don't need `Option VBASupport 1`; for LibreOffice =7.* I do need `Option VBASupport 1`. – sphh Sep 16 '20 at 09:08