我正在尝试使用Libre Office Basic为Libre Office Calc编写用户定义的函数,该函数将Goal Seek作为函数运行,而不是我进入菜单并单击。
我希望能够做的是传递一个单元格作为公式的第一个参数,第二个参数是我想要设置公式的值,第三个参数是包含变量的单元格被操纵。
例如,我想在calc =GSeek2(A1,5,A2)
中键入单元格A3,其中A1包含=A2 + 1
,A2从0开始,A3等于4。
我认为要操纵的公式和单元格需要通过Goal Seek函数作为地址传递,但我不确定。我尝试了以下代码,但我不断收到“BASIC语法错误。意外的符号:目标。”谁知道我哪里出错?
Function GSeek2(Form As Range, Target as Double, Var as Range)
Form = Form.Address
Var = Var.Address
With Worksheets("Sheet1")
GSeek2 = .Range(Form).GoalSeek _
Goal:=.Value(Target).Value, _
ChangingCell:=.Range(Var)
End With
End Function
答案 0 :(得分:0)
Openoffice或libreoffice宏不能像在Excel中一样用VBA编写。所以你的VBA代码不起作用。您必须使用特殊的BASIC方言和openoffice或libreoffice的特殊API。好的起点是https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide
不幸的是,您无法在openoffice或libreoffice中的用户定义函数中将对象作为参数传递。因此,您只能传递单元格地址(工作表,列,行)的单个部分作为参数。
示例:
Function GSeek2(lSheetF as long, lColF as long, lRowF as long, lSheetV as long, lColV as long, lRwoV as long, dTarget as double) as double
Dim oCellAddressFormula as new com.sun.star.table.CellAddress
oCellAddressFormula.Sheet = lSheetF-1
oCellAddressFormula.Column = lColF-1
oCellAddressFormula.Row = lRowF-1
Dim oCellAddressVaiable as new com.sun.star.table.CellAddress
oCellAddressVaiable.Sheet = lSheetV-1
oCellAddressVaiable.Column = lColV-1
oCellAddressVaiable.Row = lRowV1-1
oGoalResult = ThisComponent.seekGoal(oCellAddressFormula, oCellAddressVaiable, dTarget)
GSeek2 = oGoalResult.Result
End Function
在单元格中使用
=GSEEK2(SHEET(A1),COLUMN(A1),ROW(A1), SHEET(A2),COLUMN(A2),ROW(A2), 5)