This macro will calculate the average of cells G31 through G42 on Sheet1 and place the result in G45. The phrase “Average_Marks” will be placed in cell H45:
dim oFunction as variant
dim aArgument(0) as variant
dim oSheet as Object
dim myRange as Object
dim TotalCount as Double
oFunction = createUnoService("com.sun.star.sheet.FunctionAccess")
'Sheet to be used'
oSheet = ThisComponent.Sheets.getByName("Sheet1")
'Range for the function'
myRange = oSheet.getCellRangeByName( "G31:G42" )
'Set arguement for function'
'Run & get result of function'
TotalCount = oFunction.callFunction( "Average", aArgument )
'Place result by getting location & setting Value'
myRange = oSheet.GetCellRangeByName("G45")
'Place wording by getting location & setting String'
myRange = oSheet.GetCellRangeByName("H45")
You may want to review some documentation on macros. You can start with Calc Macros