Function accessing cell range

筅森魡賤 提交于 2019-12-05 14:28:49

With Calc spreadsheets it is not possible to hand over CellRange objects as parameters to User Defined Functions. If you give a cell range as the parameter, then this will be always a variant array. So there are two possibilities.

Either you need the cell values in the function, then you can take the variant array and use it:

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

Can be used as: =CHECKBZRANGE(A6:C9)

Or you need really the cell range object, then you must give its positions as parameters:

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

Can be used as: =CHECKBZRANGE2(COLUMN(A6);ROW(A6);COLUMN(C9);ROW(C9))

Hint: This will only be recalculated if either A6 or C9 has changed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!