Cell Range Access Function

I do not find how to use cell ranges with functions.

I searched in vain for some examples.

I wrote the following test. I get an "Object variable not set" error for the strings for "(one without" RangeAddress ", and the second with it, because I'm not sure about the correct syntax):

function CHECKBZRANGE(cellRange) as integer
    dim nCol as integer
    dim nLine as integer
    dim i as integer

    for nCol = cellRange.StartColumn to cellRange.EndColumn
        for nLine = cellRange.RangeAddress.StartRow to cellRange.RangeAddress.EndRow
            i = i + 1      ' placeholder for some computation
        next nLine
    next nCol
    checkBZ_range = i
end function

This function is called using a cell of type =CHECKBZRANGE(A6:C9)

Can someone explain how to use the cell range passed by argument?

+4
source share
1 answer

Calc CellRange . , . , .

, :

public function CHECKBZRANGE(vCellRangeValues as variant) as integer

    dim i as integer
    dim vCellValue as variant

    if not isarray(vCellRangeValues) then
        vCellValue = vCellRangeValues
        msgbox vCellValue
        i = i + 1
        CHECKBZRANGE = i
        exit function
    end if

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

    CHECKBZRANGE = i
end function

: =CHECKBZRANGE(A6:C9)

, :

public function CHECKBZRANGE2(lcol1 as long, lrow1 as long, lcol2 as long, lrow2 as long ) as integer

    dim i as integer
    dim oCellRange as object
    dim lRow as long
    dim lCol as long
    dim oCell as object

    oCellRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(lcol1-1,lrow1-1,lcol2-1,lrow2-1)

    for lCol = 0 to oCellRange.Columns.Count -1
     for lRow = 0 to oCellRange.Rows.Count -1
        oCell = oCellRange.getCellByPosition(lCol, lRow)
        msgbox oCell.AbsoluteName
        i = i + 1
     next
    next

    CHECKBZRANGE2 = i
end function

: =CHECKBZRANGE2(COLUMN(A6);ROW(A6);COLUMN(C9);ROW(C9))

: A6 C9.

+5

Source: https://habr.com/ru/post/1611118/


All Articles