VLOOKUP XLOOKUP & cell Note / Annotation problems

I have sheets “Market” & “Products_id_names” ( CellRange “A2:B1048576” :face_with_hand_over_mouth: )
I add “market” list/rows ( with only products id numbers (col. C) and other data (col. D, E, …) ).

I’m traying uselessly / to no avail :

=VLOOKUP(C2;Products_id_names!A:B;2;FALSE)

Even when pointing to locations in the VLOOKUP function window with the mouse cursor, it does not return a result ("#NAME?")

I finally found a way/solution to display names corresponding to id numbers (solution) like this:

=XLOOKUP(C2;$Products_id_names.A:A;$Products_id_names.B:B; ;0;1)

Since the function only displays the id names and you can’t just copy-paste the names elsewhere, I still had to use a macro (BASIC).
There were several attempts to do this (initially, to change the id number to a name and add a comment (note , Annotation ) to the id number cell).
The macro did the name change, but there were problems with adding the comment (it only added/changed the ID number to the cell where there was already a comment before, but it did not add a comment to the cell without a comment).

Sub ReplaceIDwithNameAndAddNote()
	Dim oDoc As Object
	Dim oSheetMarket As Object
	Dim oSheetProducts As Object
	Dim oCell As Object
	Dim oRange As Object
	Dim oAnnotation As Object
	Dim i As Integer
	Dim j As Integer
	Dim lookupID As String
	Dim foundName As String

	oDoc = ThisComponent
	oSheetMarket = oDoc.Sheets.getByName("Market")
	oSheetProducts = oDoc.Sheets.getByName("Products_id_names")

	For i = 2 To 100
		oCell = oSheetMarket.getCellByPosition(2, i-1)
		lookupID = oCell.String

		If lookupID <> "" Then
			oRange = oSheetProducts.getCellRangeByName("A:B")
			foundName = ""

			For j = 0 To oRange.Rows.Count - 1
				If oRange.getCellByPosition(0, j).String = lookupID Then
					foundName = oRange.getCellByPosition(1, j).String
					Exit For
				End If
			Next j
			If foundName <> "" Then
				oAnnotation = oCell.createAnnotation
				oAnnotation.String = "ID:" & lookupID ' <---- add NOTE to cell NOT WORK
				oCell.String = foundName
			End If
		End If
	Next i
End Sub

and

Does not add a comment to a cell if there was no previous comment, but changes the comment on the cell if that cell previously had a comment.

            If foundName <> "" Then
                If oCell.Annotation Is Nothing Then
                    oAnnotation = oCell.createAnnotation
                    oAnnotation.String = "ID: " & lookupID
                Else
                    oCell.Annotation.String = "ID: " & lookupID
                End If
                oCell.String = foundName
            End If

and

            If foundName <> "" Then
                oCell.String = foundName
                
                If oCell.Annotation Is Nothing Then
                    oAnnotation = oDoc.createInstance("com.sun.star.sheet.CellAnnotation")
                    oAnnotation.String = "ID: " & lookupID
                    oCell.Annotation = oAnnotation
                    oAnnotation.setPosition(oCell.getPosition)
                Else
                    oCell.Annotation.String = "ID: " & lookupID
                End If
            End If

and (not work ( BASIC runtime error. Property or method not found: setAnnotation. ) )

        If lookupID <> "" Then
            oRange = oSheetCommodities.getCellRangeByName("A:B")
            foundName = ""

            For j = 0 To oRange.Rows.Count - 1
                If oRange.getCellByPosition(0, j).String = lookupID Then
                    foundName = oRange.getCellByPosition(1, j).String
                    Exit For
                End If
            Next j
            
            If foundName <> "" Then
                oCell.String = foundName
                oCell.setAnnotation("ID: " & lookupID)
                
                If Not oCell.Annotation Is Nothing Then
                    oCell.Annotation.IsVisible = True
                End If
            End If
        End If

and not work

	If foundName <> "" Then
		oCell.String = foundName
		If oCell.Annotation Is Nothing Then
			On Error Resume Next
			Set oAnnotation = oDoc.createInstance("com.sun.star.sheet.CellAnnotation")
			If Not oAnnotation Is Nothing Then
				oAnnotation.String = "ID: " & lookupID
				oCell.Annotation = oAnnotation
				oAnnotation.setPosition(oCell.getPosition)
				oAnnotation.IsVisible = True
			Else
				MsgBox "error" & i
			End If
				On Error Goto 0
		Else
			oCell.Annotation.String = "ID: " & lookupID
			oCell.Annotation.IsVisible = True
		End If
	End If

:heart_eyes: :smiling_face_with_three_hearts: :innocent: :tada:
In the end, I decided that the macro would not change the id number cells, but would create new cells, and then I finally found the desired result, a working solution:

REM ' =XLOOKUP(C2;$Products_id_names.A:A;$Products_id_names.B:B; ;0;1)

Sub AddNameToMarketColumnBAndAddNoteFromProducts()
	Dim oDoc As Object
	Dim oSheetMarket As Object
	Dim oSheetProducts As Object
	Dim oCell As Object
	Dim oCellB As Object
	Dim oRange As Object
	Dim oAnnotations As Object
	Dim oCellAddress As New com.sun.star.table.CellAddress
	Dim i As Integer
	Dim j As Integer
	Dim lookupID As String
	Dim foundName As String
	Dim hasAnnotation As Boolean
	
	oDoc = ThisComponent
	oSheetMarket = oDoc.Sheets.getByName("Market")
	oSheetProducts = oDoc.Sheets.getByName("Products_id_names")
	
	oAnnotations = oSheetMarket.getAnnotations()
	
	REM Debug: I will delete the entire contents of column B starting from row 2, keeping B1
	oSheetMarket.getCellRangeByName("B2:B1048576").clearContents(7) ' 7 = VALUE + STRING + FORMULA
	
	' assume that the IDs are in column C (index 2) starting from row 2
	For i = 2 To 100 ' Change 100 to the number of rows in your data.
		oCell = oSheetMarket.getCellByPosition(2, i-1) ' column C (index 2) row i
		lookupID = oCell.String
		
		' If the cell is not empty
		If lookupID <> "" Then
			REM Debug: Processing ID: & lookupID
			' Look for IDs in the Products_id_names page
			oRange = oSheetProducts.getCellRangeByName("A:B")
			foundName = ""
			
			' Find ID match
			For j = 0 To oRange.Rows.Count - 1
				If oRange.getCellByPosition(0, j).String = lookupID Then
					foundName = oRange.getCellByPosition(1, j).String
					Exit For
				End If
			Next j
			
			' If the name is found, write the name in column B and add a note.
			If foundName <> "" Then
				REM Debug: Found name for ID & lookupID & : & foundName
				' Write the name in column B (index 1)
				oCellB = oSheetMarket.getCellByPosition(1, i-1)
				oCellB.String = foundName
				
				' Check if the cell in column B has a comment
				On Error Resume Next
				hasAnnotation = Not oCellB.Annotation Is Nothing And Len(oCellB.Annotation.String) > 0
				On Error Goto 0
				
				' Set the cell address for column B
				oCellAddress.Sheet = oSheetMarket.RangeAddress.Sheet
				oCellAddress.Column = oCellB.CellAddress.Column
				oCellAddress.Row = oCellB.CellAddress.Row
				
				If Not hasAnnotation Then
					' Add a new note in the format: id number: [ID]\n[FoundName]
					On Error Resume Next
					oAnnotations.insertNew(oCellAddress, "" & lookupID & Chr(10) & foundName) ' "id number: " &
					If Err = 0 Then
						REM Debug: New note added to cell B & i & : id number: & lookupID & & foundName
					Else
						REM Debug: Error: Unable to add comment to cell B & i
					End If
					On Error Goto 0
				Else
					' Update an existing note
					On Error Resume Next
					oCellB.Annotation.String = "" & lookupID & Chr(10) & foundName ' "id number: " &
					If Err = 0 Then
						REM Debug: Note on updated box B & i & : id number: & lookupID & & foundName
					Else
						REM Debug: Error: Failed to update comment for cell B & i
					End If
					On Error Goto 0
				End If
				
				' Check if the note was actually added
				On Error Resume Next
				If Not oCellB.Annotation Is Nothing And Len(oCellB.Annotation.String) > 0 Then
					REM Debug: Check: Note present in box B & i & : & oCellB.Annotation.String
				Else
					REM Debug: Check: No comment in box B & i
				End If
				On Error Goto 0
			Else
				REM Debug: Name not found for ID: & lookupID
			End If
		Else
			REM Debug: Column C cell in empty row & i
		End If
	Next i
	
	MsgBox "Macro finished working! Check the names in column B and the comments.", 64, "Notice"
End Sub

Why was it so difficult to find a solution to add a comment and where is the detailed manual for adding a comment to a macro BASIC cell?

Some older Office (Free Open Excel xD) used a different way of adding a comment to a cell? How can I debug such a problem better, to find out why it doesn’t add a comment (my debugging was as if there was already a comment) (now I think maybe the order of the commands was wrong, first you have to add new content to the cell or after that and then the comment or before)

This is what I needed for Eve Online :rocket: :ringer_planet: :rocket: :milky_way: :artificial_satellite: :comet: :space_invader: game marketing ( Presonal Market Export information ( C:\Users\kati\Documents\EVE\logs\Marketlogs\My Orders-2025.10.08 2134.txt ) order typeID column to to change/add the product name ( I copied the database adam4eve.eu/info_types.php ) and to be OffLine (Eve-Offline :face_with_hand_over_mouth: ) (without using an online database or excel SDE) )

try Calc addressing

=VLOOKUP(C2;Products_id_names.A:B;2;FALSE)

BiG PROBLEM’S ( Noobs dont know this so easy ):
Error: If the cursor is inside (not the main function but an additional function) e.g. Function Min() and you run a macro, you get the error:

BASIC runtime error.
Argument is not optional.

Cause: In LibreOffice Basic, macro execution attempts to execute the function on which the cursor is located. Function Min(a As Long, b As Long) expects two parameters (a and b), but when you run it directly, no parameters are provided, which is why the error occurs. If the cursor is inside Sub AddNameToMarketColumnBAndAddNoteWithVLOOKUP(), the main macro runs correctly, as it does not require arguments.
Solution: Make sure that the cursor is inside Sub (or select the exact name of the macro from Tools > Macros > Run Macro). This explains the previous anomalies, as running the wrong function can cause unexpected errors.


( code work w oFunction.callFunction(“VLOOKUP”, Array( max 2 sec and w/o VLOOKUP +10sec ( Products_id_names table have A1:C25993 rows data (id+names) . In addition to increasing speed, it creates a cache table so that you don’t have to use a larger table every time for product names that have already been found. Which already gave a noticeable result (work time) even without VLOOKUP. ) )

Sub AddNameToMarketColumnBAndAddNoteWithVLOOKUP()
	Dim oDoc As Object
	Dim oSheetMarket As Object
	Dim oSheetProducts As Object
	Dim oSheetCache As Object
	Dim oCell As Object
	Dim oCellB As Object
	Dim oAnnotations As Object
	Dim oCellAddress As New com.sun.star.table.CellAddress
	Dim oFunction As Object
	Dim i As Long ' Muudetud Integer -> Long '
	Dim lookupID As String
	Dim foundName As Variant
	Dim lastRow As Long
	Dim cacheLastRow As Long

	oDoc = ThisComponent ' Määra dokumendi ja lehtede objektid

' If oDoc Is Nothing Then
' MsgBox "Error: oDoc is Nothing!", 16, "Debug Error"
' Exit Sub
' End If

	On Error Resume Next
	oSheetMarket = oDoc.Sheets.getByName("Market")
	If oSheetMarket Is Nothing Then 
	MsgBox "Error: oSheetMarket is Nothing! Check if sheet 'Market' exists.", 16, "Debug Error"
		Exit Sub
	End If

	oSheetProducts = oDoc.Sheets.getByName("Products_id_names")
	If oSheetProducts Is Nothing Then 
	MsgBox "Error: oSheetProducts is Nothing! Check if sheet 'Products_id_names' exists.", 16, "Debug Error" 
	Exit Sub 
	End If

	oSheetCache = oDoc.Sheets.getByName("Products_id_names_cache")
	If Err.Number <> 0 Then REM Debug: Lehte Products_id_names_cache ei leitud, loon uue
		oDoc.Sheets.insertNewByName("Products_id_names_cache", oDoc.Sheets.Count)
		oSheetCache = oDoc.Sheets.getByName("Products_id_names_cache")
		oSheetCache.getCellByPosition(0, 0).String = "ID"
		oSheetCache.getCellByPosition(1, 0).String = "Name"
	End If
	On Error Goto 0

	' Hangi FunctionAccess teenus
	oFunction = createUnoService("com.sun.star.sheet.FunctionAccess")
	If oFunction Is Nothing Then
		MsgBox "Error: oFunction is Nothing!", 16, "Debug Error"
		Exit Sub
	End If

	oAnnotations = oSheetMarket.getAnnotations() ' Hangi Market lehe märkuste objekt

	lastRow = 3 ' Leia viimane täidetud rida lehel Market
	While oSheetMarket.getCellByPosition(3, lastRow-1).String <> "" REM Debug: Otsin viimast täidetud rida lehel Market
		lastRow = lastRow + 1
	Wend
	If lastRow < 3 Then lastRow = 3
	' MsgBox "lastRow = " & CStr(lastRow) & " (Type: " & TypeName(lastRow) & ")", 64, "Debug lastRow"

	' Kustuta tulba B sisu ja kommentaarid alates reast 3 kuni viimase täidetud reani
	REM Debug: Kustutan tulba B sisu ja kommentaarid alates reast 3 kuni rida & lastRow
	oSheetMarket.getCellRangeByName("B3:B" & lastRow).clearContents(7) ' 7 = VALUE + STRING + FORMULA
	oSheetMarket.getCellRangeByName("B3:B" & lastRow).clearContents(8) ' 8 = ANNOTATION

	' Leia viimane täidetud rida cache-tabelis
	cacheLastRow = 2 REM Debug: Otsin viimast täidetud rida lehel Products_id_names_cache
	While oSheetCache.getCellByPosition(0, cacheLastRow-1).String <> ""
		cacheLastRow = cacheLastRow + 1
	Wend
	If cacheLastRow < 2 Then cacheLastRow = 2
	' MsgBox "cacheLastRow (before loop) = " & CStr(cacheLastRow) & " (Type: " & TypeName(cacheLastRow) & ")", 64, "Debug cacheLastRow"

	' Töötle ID-d tulbas D alates reast 3 kuni viimase täidetud reani (või max 100)
	For i = 3 To Min(lastRow, 100)
		' MsgBox "Processing row i = " & CStr(i) & " (Type: " & TypeName(i) & ")", 64, "Debug Loop"
		oCell = oSheetMarket.getCellByPosition(3, i-1)
		lookupID = CStr(oCell.String)

		If lookupID <> "" Then REM Debug: Töötlen ID-d: & lookupID
			foundName = Empty
			' Otsi esmalt cache-tabelist VLOOKUP abil
			On Error Resume Next REM Debug: Otsin ID-d & lookupID & lehelt Products_id_names_cache
			foundName = oFunction.callFunction("VLOOKUP", Array(lookupID, oSheetCache.getCellRangeByName("A2:B" & cacheLastRow), 2, False))

			If Err.Number = 0 And Not IsEmpty(foundName) Then REM Debug: Leitud nimi cache-st ID-le & lookupID & : & foundName
			Else
				foundName = Empty REM Debug: ID-d & lookupID & ei leitud cache-st VLOOKUP abil
			End If
			On Error Goto 0

			' Kui cache-st ei leitud, otsi Products_id_names lehelt
			If IsEmpty(foundName) Then REM Debug: Otsin ID-d & lookupID & lehelt Products_id_names
				On Error Resume Next
				foundName = oFunction.callFunction("VLOOKUP", Array(lookupID, oSheetProducts.getCellRangeByName("B2:C25993"), 2, False))
				If Err.Number = 0 And Not IsEmpty(foundName) Then
					REM Debug: Leitud nimi Products_id_names lehelt ID-le & lookupID & : & foundName
					' MsgBox "Adding new ID to cache at row = " & CStr(cacheLastRow) & " (Type: " & TypeName(cacheLastRow) & ")", 64, "Debug cacheLastRow Add"
					oSheetCache.getCellByPosition(0, cacheLastRow-1).String = lookupID
					oSheetCache.getCellByPosition(1, cacheLastRow-1).String = foundName
					REM Debug: Lisatud uus ID-nime paar cache-sse real & cacheLastRow
					cacheLastRow = cacheLastRow + 1
					' MsgBox "cacheLastRow (after add) = " & CStr(cacheLastRow) & " (Type: " & TypeName(cacheLastRow) & ")", 64, "Debug cacheLastRow After"
				Else REM Debug: Nime ei leitud ID-le: & lookupID
					foundName = Empty
				End If
				On Error Goto 0
			End If

			' Kui nimi leiti, kirjuta nimi tulpa B ja lisa märkus
			If Not IsEmpty(foundName) Then REM Debug: Kirjutan nime ID-le & lookupID & : & foundName
				oCellB = oSheetMarket.getCellByPosition(1, i-1)
				oCellB.String = foundName

				' Määra lahtri aadress ja lisa uus märkus
				oCellAddress.Sheet = oSheetMarket.RangeAddress.Sheet
				oCellAddress.Column = oCellB.CellAddress.Column
				oCellAddress.Row = oCellB.CellAddress.Row

				On Error Resume Next
				oAnnotations.insertNew(oCellAddress, "" & lookupID & Chr(10) & foundName)
				If Not Err.Number = 0 Then REM Debug: Uus märkus lisatud lahtrile B & i & : & lookupID & & foundName
					' MsgBox "Annotation added for B" & i & ": " & lookupID & Chr(10) & foundName, 64, "Debug Annotation"
				Else REM Debug: Viga: Ei õnnestunud lisada märkust lahtrile B & i
					MsgBox "Error: Failed to add annotation for B" & i, 16, "Debug Error"
				End If
				On Error Goto 0
			Else
				REM Debug: Nime ei leitud ID-le: & lookupID
			End If
		Else
			REM Debug: Tulba D lahter tühi reas & i
		End If
	Next i

	MsgBox "Macro finished working! Check the names in column B and the comments.", 64, "Notice"
End Sub

Function Min(a As Long, b As Long) As Long
	' MsgBox "Min() called: a = " & CStr(a) & " (Type: " & TypeName(a) & "); b = " & CStr(b) & " (Type: " & TypeName(b) & ")", 64, "Debug Min"
	If a < b Then
		Min = a
	Else
		Min = b
	End If
End Function

I guess I had already tried that (without success),
maybe there is a problem with the sheet name or some other stupid excuse (or some version bug)



So the only solution was =XLOOKUP()
( =XLOOKUP(D3;$Products_id_names.B:B;$Products_id_names.C:C; ;0;1) )