Macros 如何将cellrange传递给用户定义的宏参数
我想在宏中使用cellrangesMacros 如何将cellrange传递给用户定义的宏参数,macros,libreoffice-calc,Macros,Libreoffice Calc,我想在宏中使用cellranges 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
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对象而不是字符串调用这个函数。因为getCellRangeByName仅适用于字符串变量。
因为当我这样调用函数时
sumifcolor(B1:B3)
我得到了以下错误:
“未设置对象变量”
我读了一些提示,但对我没有帮助。不可能传递实际的CellRange对象。一种解决方案是传递行和列编号,类似于链接中@Axel Richter答案的第二部分:
函数SumIfColor(lcol1、lrow1、lcol2、lrow2)
总和=0
oCellRange=ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(_
lcol1-1、lrow1-1、lcol2-1、lrow2-1)
对于lCol=0到oCellRange.Columns.Count-1
对于lRow=0到oCellRange.Rows.Count-1
oCell=oCellRange.getCellByPosition(lCol,lRow)
如果oCell.CellBackColor>-1,则
总和=总和+oCell.值
如果结束
下一个
下一个
SumIfColor=sum
端函数
称之为:
=SUMIFCOLOR(COLUMN(B1:B3),ROW(B1),COLUMN(B3),ROW(B3))
由于
列(B1:B3)
的原因,只要范围B1:B3
中的值发生更改,就会重新计算总和。但是,显然,仅更改单元格的颜色不会导致重新计算。不可能传递实际的CellRange对象。一种解决方案是传递行和列编号,类似于链接中@Axel Richter答案的第二部分:
函数SumIfColor(lcol1、lrow1、lcol2、lrow2)
总和=0
oCellRange=ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(_
lcol1-1、lrow1-1、lcol2-1、lrow2-1)
对于lCol=0到oCellRange.Columns.Count-1
对于lRow=0到oCellRange.Rows.Count-1
oCell=oCellRange.getCellByPosition(lCol,lRow)
如果oCell.CellBackColor>-1,则
总和=总和+oCell.值
如果结束
下一个
下一个
SumIfColor=sum
端函数
称之为:
=SUMIFCOLOR(COLUMN(B1:B3),ROW(B1),COLUMN(B3),ROW(B3))
由于列(B1:B3)
的原因,只要范围B1:B3
中的值发生更改,就会重新计算总和。但是,仅更改单元格的颜色显然不会导致重新计算