2016-11-28 71 views
-1

REM ***** BASIC *****LibreOffice的宏觀平均計算

Sub hell_world 
    dim hell_world as object 
    hell_world = Range("G31:G42") 
    Range("H45") = WorksheetFunction.Average(hell_world) 
End Sub 

我有,我得到的錯誤:

BASIC runtime error. 
Sub-procedure or function procedure not defined. 

我該怎麼辦? 謝謝。

回答

0

使用XFunctionAccess如在Andrew Pitonyak's macro document6.28. Use FunctionAccess to call Calc Functions中所述。

Sub CallAverage 
    Dim oSheet, oRange, oCell, nResult, oFuncAcc 
    oSheet = ThisComponent.CurrentController.ActiveSheet 
    oRange = oSheet.getCellRangeByName("G31:G42") 
    oCell = oSheet.getCellRangeByName("H45") 
    oFuncAcc = CreateUnoService("com.sun.star.sheet.FunctionAccess") 
    nResult = oFuncAcc.callFunction("Average", Array(oRange)) 
    oCell.setValue(nResult) 
End Sub 
+0

我得到錯誤: BASIC運行時錯誤。 找不到屬性或方法:ActiveSheet。 –

+0

確保從Calc電子表格中運行宏,而不是從Basic窗口或Writer運行。另一種選擇是像這樣寫:'oSheet = ThisComponent.Sheets(0)'。但是,這隻會在'ThisComponent'是一個電子表格時才起作用。 –