Welcome!
Let’s start by noting that you don’t need a macro for this task. Recently a similar problem was solved here
But if you want to do it with a macro, then let’s analyze your code.
First of all, why exactly 9999 rows? If the list ever gets longer, you’ll have to change the macro code. It is better to immediately use the code that will find all the data on the sheet - this function has already been written, it is located in the Misc module of the Tools library and looks like this:
Function GetLastUsedRow(oSheet as Object) as Long
Dim oCell As Object
Dim oCursor As Object
Dim aAddress As Variant
oCell = oSheet.GetCellbyPosition(0, 0)
oCursor = oSheet.createCursorByRange(oCell)
oCursor.GotoEndOfUsedArea(True)
aAddress = oCursor.RangeAddress
GetLastUsedRow = aAddress.EndRow
End Function
Now about .getcellbyposition(1, a).value
. This works well, but slowly. If you need to read / write a dozen cells, then you can use this method. But for large data sets, it is better to read the entire set into an array at one time, process it, and again write the result in one go. It will be faster, much faster! This is done with .getDataArray()
and .setDataArray(oData)
. For example, look at the code from this message - it also selects data from a long list by condition
Using activesheet
. The bad news is that StarBasic doesn’t know that word. Good news - yes, the current sheet can be obtained like this:
oActiveSheet = ThisComponent.getCurrentController().getActiveSheet()
The construct “.getcellbyposition(1,b) = something” makes sense because it means “replace the cell of the sheet (Object !) with something else”. Therefore, BASIC does not say that this is a mistake. Another thing is that Calc categorically objects when someone touches his cells - so nothing happens. What you actually meant was “change the value in the cell to something”. This is written differently:
oTargetSheet.getcellbyposition(0,b).setValue(oSourceSheet.getcellbyposition(0,a))
…or .setString(), or .setFormula()
One last note… You didn’t take care to clear the sheet of old data before writing new data to it using the macro. If you run a macro for a customer with more orders, and then for a customer with fewer rows, the resulting sheet will have the second customer’s data and some of the first customer’s data… Look at the clearContents() method
Ask more questions please