Windows 11 Pro (Build 26100)
Libre Office Version: 24.8.4.2 (X86_64)
I may not need to say this because many of you have replied to my posts before but, I am not a coder and a few on here have helped me a great deal in the past. I have learned a little here and there but I really don’t create macros that often to get used to using it.
So, despite not wanting to ask/bother you all again to help me set up a macro code (to help replace the macros that were deleted during an update for Libre Office) I decided to use ChatGPT, and here I am again.
I am getting a BASIC runtime error from the code it gave me. I told ChatGPT about it and it changed the code but even the correction is getting the same error.
This is the full error: BASIC runtime error. Property or method not found: getRange
This is the code:
Sub AddStatNewToStatCurr
Dim oDoc As Object
Dim oSheet As Object
Dim oStatNew As Object
Dim oStatCurr As Object
Dim i As Long
Dim numRows As Long
Dim statNewCell As Object
Dim statCurrCell As Object
' Get the current document and sheet
oDoc = ThisComponent
oSheet = oDoc.Sheets(0) ' Change to the relevant sheet if needed
' Access the named ranges StatNew and StatCurr
oStatNew = oDoc.NamedRanges.getByName("StatNew")
oStatCurr = oDoc.NamedRanges.getByName("StatCurr")
' Get the range of StatNew
Dim statNewRange As Object
statNewRange = oStatNew.getRange()
' Get the number of rows in StatNew (assuming both ranges have the same number of rows)
numRows = statNewRange.Rows.Count
' Loop through each row and add the values
For i = 0 To numRows - 1
' Get the corresponding cell in StatNew and StatCurr
statNewCell = statNewRange.getCellByPosition(0, i) ' StatNew column, row i
statCurrCell = oStatCurr.getRange().getCellByPosition(0, i) ' StatCurr column, row i
' Add the value from StatNew to StatCurr and replace StatCurr value
statCurrCell.Value = statCurrCell.Value + statNewCell.Value
Next i
End Sub
I want this macro (linked to a button) to take the numbers in the named column of StatNew and add them to the numbers in the named column of StatCurr, then replace the current numbers in StatCurr with the new total.