如何将单元格范围传递给用户定义的宏参数

how to pass cellrange to a user defined macro paramenter

我想在我的宏中使用单元格范围。

Function SumIfColor(SumRange)
    Dim oRange as object
    Dim oSheet as object

    ' Get Access to the Active Spreadsheet
    oSheet = ThisComponent.CurrentController.ActiveSheet

    ' Get access to the Range listed in Sum Range
    oRange = oSheet.getCellRangeByName(SumRange).RangeAddress
End Function

问题是如何使用真正的 cellRange 对象而不是 String 调用此函数。因为 getCellRangeByName 仅适用于 String 变量。 因为当我这样调用函数时

sumifcolor(B1:B3)

我收到以下错误: "Object variable not set"

我读了一些提示 但它对我没有帮助。

无法传递实际的 CellRange 对象。一种解决方案是传递行号和列号,类似于@Axel Richter 在 link:

中回答的第二部分
Function SumIfColor(lcol1, lrow1, lcol2, lrow2)
    sum = 0
    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)
        If oCell.CellBackColor > -1 Then
            sum = sum + oCell.Value
        End If
     Next
    Next
    SumIfColor = sum
End Function

调用它:

=SUMIFCOLOR(COLUMN(B1:B3),ROW(B1),COLUMN(B3),ROW(B3))

由于 COLUMN(B1:B3),只要 B1:B3 范围内的值发生变化,就会重新计算总和。但是,显然仅更改单元格的颜色不会导致重新计算。