Create an macro to search specific data

Hello

OS Ubuntu 18.04
LibreOffice 6.0*

I need a help with creating macro which will do next

This is an example how to write code in Basic

DIM SomeText(5) as text

rem -- I'm looking that strings
SomeText(1)="Audi"
SomeText(2)="Alfa"
SomeText(3)="BMW"
SomeText(4)="Citroen"
SomeText(5)="Mercedes"
rem etc ....

rem in column A there are more strings. I'm looking for prices just for those cars!

rem -- now loop 
FOR count = 1 to 5

rem -- put value from cell to variable SellPrice
  SellPrice=INDEX($E$14:$E$41;MATCH(SomeText(count);$A$14:$A$41;0))

rem -- write SellPrice to cell H8 (to cell ADDRESS(8; column() count + 7)
  WriteToCell H8 SellPrice

rem -- put value from cell to variable BuyPrice
  BuyPrice=INDEX($D$14:$D$41;MATCH((SomeText(count);$A$14:$A$41;0))

rem -- write BuyPrice to cell H10 to cell ADDRESS(10; column() count + 7)
  WriteToCell H10 SellPrice

  Format cell $H$8:$L$10 as Currency justify right

NEXT

Regards

Sample.ods

Where is Сell xy located?

For example in row 8 and 10 and column from H to Q

I’m not sure I understand better.

Find 11 (or is it 12?) text strings among 28 cells (A14:A41) and place the corresponding values ​​from column E in a rectangle of 30 cells H8:Q10? We are not talking about formatting yet.

@njupalo I formated you code a bit … hope you dont mind.

Ok, I looked at the sample data. Why you think the formula solution is worse than the macro solution?
C:\fakepath\Search specific data (real spreadsheet).ods

Take out the names from the formulas and place them in cells, add drop-down lists for convenient selection of the necessary positions, format the cells - the formulas will do the rest.
ChooseItem.png

Are you worried that you get wrong formulas when copying and pasting into another document? Don’t paste formulas! Use Ctrl+Shift+V (Paste Special) and paste only values.

Well I have to find 22 item every day! And writing formulas or even copy it from another document is not what I like to do.

I don’t understand - why are you going to copy formulas? Copy data! Save your formulas book. When new data comes, copy it into this book and get the result instantly. I must admit that you have said very little about the ultimate goal and the methods of achieving it that you are using now.

Formulas book???
I don’t know what is this! :frowning:

Every day I get a file with prices. Sell and Buy! From 25 or 28 text item I’m interested for 11. So I need 11 buy prices and 11 sell prices. Those prices I copy to another document which is different formated.

“Formula book” is “Find specific data (real table) .ods” from the previous comment. Get new prices, open it, select all and copy. Then open the “Formula Book” (the same every day, always), paste the data from the new prices starting A1. Select the results in H8:Q10, copy. Switch to the target document (where do you insert new prices every day?) and paste values ​​and formats using Ctrl+Shift+V.

This works with your sample file. The comments should make it self explaining. I hope.

rem helper  to change the cell format ... shamelessly copied from the macro bibel. 
Function FindCreateNumberFormatStyle (sFormat$, Optional oDoc, Optional locale)
  Dim oDocument As Object
  Dim aLocale as new com.sun.star.lang.Locale
  Dim oFormats As Object
  Dim formatNum As Long
  oDocument = IIf(IsMissing(oDoc), ThisComponent, oDoc)
  oFormats = oDocument.getNumberFormats()
  'I could set the locale from values stored at
  'http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt
  'http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
  'I use a NULL locale and let it use what ever it likes.
  'First, see if the number format exists
  If ( Not IsMissing(locale)) Then
    aLocale = locale
  End If
  formatNum = oFormats.queryKey (sFormat, aLocale, TRUE)
  'MsgBox "Current Format number is" & formatNum
  'If the number format does not exist then add it
  If (formatNum = -1) Then
    formatNum = oFormats.addNew(sFormat, aLocale)
    If (formatNum = -1) Then formatNum = 0
    'MsgBox "new Format number is " & formatNum
  End If
  FindCreateNumberFormatStyle = formatNum
End Function

rem magic subroutine 
SUB DOSTUFF
    needles = Array("audi","alfa", "bmw","citroen", "mercedes")
	searchcol = "A" rem search in this column
	startrow = 6    rem start search in this row
	endrow = 17	rem end searching in this row
	putcol=7 rem start colum to insert data (here: A=1, B=2, ... H=7, ...)
	putrow=7	rem startrow to put data
	formatcode = "#,##0.00 [$EUR];-#,##0.00 [$EURO]" 
	oSheet = ThisComponent.CurrentController.ActiveSheet
	Dim val as double
	For i = startrow To  endrow
		For j = LBound(needles) To  UBound(needles)
		Set oCell = oSheet.getCellRangeByName(searchcol & i)
			if ( InStr(1, oCell.String, needles(j), 1)  > 0 ) then
			    rem copy value and change cell format 
				val = oSheet.getCellRangeByName("E" & i ).Value
				oSheet.getCellByPosition( putcol, putrow+0 ).Value= val
				oSheet.getCellByPosition( putcol, putrow+0 ).NumberFormat  = FindCreateNumberFormatStyle(formatcode, ThisComponent)
			    rem copy value and change cell format 
 				val = oSheet.getCellRangeByName("D" & i ).Value
				oSheet.getCellByPosition( putcol, putrow+2 ).Value= val
				oSheet.getCellByPosition( putcol, putrow+2 ).NumberFormat  = FindCreateNumberFormatStyle(formatcode, ThisComponent)
				putcol=putcol+1
			endif
		Next
	Next  
End SUB

If you have any further question, just ask.

Hope that helps.


To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!


Have a nice day and let’s (continue to) “Be excellent to each other!”


Ask / Getting Started:

https://wiki.documentfoundation.org/Ask/Getting_Started

Thank you for your macro, which unfortunately is not usefull for me.
Look at my basic code.
I need data from column A, D and E.
I don’t know how to attach picture to show you what I need!

Regards

Please correct my if i am wrong, but your pseudo code, just searches for a “substring” in a set range of cells and on finding that substring it copies the data of the cell it found to another cell (xy), right? If thats it, i think i can adjust my function to your needs easily.

I am searching string in column A. If found copy value in that row from column D and E to another two cells.
example:
row A found “some string” then copy value from that row and column D to H8 and from column E to H10.

Wait, whats the logic here?
A → D fine
But what about H8 and E
i dont get the sequence/connection, sorry.

By the way nothing in your pseudocode indicated that you wanted to copy multiple times, Maybe just update the pseudocode.

A D E H

1

2 “text I’m looking for” 50 60

3

4

8 60

10 50

In cell A2 I found string. In cell D2 is value and E2 is value.

Copy from D2 to H8 and from E2 to H10

What if i find a value at A4 ?

Copy From D4 to H12 and E4 to H14 ?

Is that the squence?

H is 6 rows ahead ?

E is 8 rows ahead?

Is that correct?

https://ibb.co/9vHPXdx
https://ibb.co/B61nLMY
You can see what I wont on these two images!

Yes that is correct!

Based on that i’ll update my answer. Please stand by.

[update]
Done, but i am not sure, that it is really what you wanted. You have to check yourself.