¿Object variable not set?

I have this error:
BASIC runtime error.
Object variable not set

And this is the code:

REM  *****  BASIC  *****

Private Sub Average()
dim Total_Marks as Integer
Total_Marks = Application.WorksheetFunction.Average(Sheet1.Range("G" & 31 & ":G" & 42))
ThisWorkbook.Worksheets("Sheet1").Cells(8,45).Value = Average_Marks

End Sub

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:

Sub SetAverage
    dim oFunction as variant
    dim aArgument(0) as variant
    dim oSheet as Object
    dim myRange as Object
    dim TotalCount as Double
'Need service'
    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")
End sub

You may want to review some documentation on macros. You can start with Calc Macros

I get this error
BASIC runtime error.
An exception occurred
Type: com.sun.star.container.NoSuchElementException
Message: .
on oSheet = …

That is the sheet name (the name on the TAB at bottom of sheet) on which the cells are present. Did you give the sheet a different name? Works here on Mint 18. Where are you putting the macro? How are you executing it - from a push button or from basic? Tried both ways and it works here. I can the same message if I put in an unknown sheet name.