Plaese help with this macro, no error but no output

i want ot get list with name of specfic customer who buy specific item and quatity on date
i have list of customer with in format of date , name , item and quatity
now i want to extract only specifice customer with there purchase habit for example a customer
name xyz in cell b2 and b4 and b8 buy abc in 2 ,4 ,6 quantity with b3 ,b5,b6,b7 filled by other customer

i want only customer xyz 's entry to show in next sheet

Sub Main

   dim sheets as object
   dim sheet  as object
   dim b
   dim a
   
sheets = ThisComponent.Sheets
sheet = sheets.getByName("Sheet1")
b = 1

for a = 1 to 9999

  if sheet.getcellbyposition(1, a).value = "xyz" then 
    activesheet.getcellbyposition(1,b) = sheet.getcellbyposition(1,a)
    activesheet.getcellbyposition(0,b) = sheet.getcellbyposition(0,a)
    activesheet.getcellbyposition(2,b) = sheet.getcellbyposition(2,a)
    activesheet.getcellbyposition(3,b) = sheet.getcellbyposition(3,a)
    b = b + 1

  endif
next a

End Sub
→Data → Filter → Standardfilter ……… with output to Sheet2.A1

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

2 Likes