功能访问单元格范围

时间:2015-10-10 21:12:20

标签: libreoffice-calc libreoffice-basic

我没有找到如何使用函数的单元格范围。

我徒劳地搜索了一些例子。

我写了以下测试。我在两个“for”行中得到“Object variable not set”错误(一个没有“RangeAddress”,第二个是它,因为我不确定正确的语法):

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

使用像=CHECKBZRANGE(A6:C9)

这样的单元格调用此函数

有人可以解释如何使用参数传递的单元格区域吗?

2 个答案:

答案 0 :(得分:6)

使用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))

提示:如果A6C9发生了变化,则只会重新计算。

答案 1 :(得分:1)

是的。有可能的!只需激活与VBA的兼容性即可。

Option VBASupport 1

Option Compatible

Function Addressing(R As Range)

Addressing = R.Address

End Function