My Excel VBA functions works on text values but when the lookup value contains numbers only, the XLOOKUP can't find a match.
Range(SelectedCell) = Application.XLOOKUP("*" & Range(LookupCell) & "*", Worksheets("Stuklijst").Range(Mod_SelectColumns.StuklijstRefRange.Address), Worksheets("Stuklijst").Range(Mod_SelectColumns.StuklijstMPNCol.Address), "NOT FOUND", 2)
I tried adding a format function to the Lookup Value and Search Array and I tried storing the cell data as a Number, Text and General. Next to that I tried adding a Format function to the lookup value and lookup array part of the Xlookup function.
When I execute an XLOOKUP manually on the Cell values in one of the sheets, the function works as expected.
What am I doing wrong and how can I fix it?
Edit: I replicated my issue with some generated data
- Original data before executing the XLOOKUP the Articenumber column is empty. The numeric values are in the General Number format, such are the other values.
The LookupSheet. Just as the picture above the datatype of the numeric and other Values are General.
As you can the XLOOKUP function can't find the numeric values and results in "NOT FOUND" (Yes I have checked for spaces at the end of value). The code I use to find the Articlenumbers in the LookupSheet (the button calls the subroutine):
Sub XLOOKUPTEST()
Dim rangeMpn As Range
Dim rangeArtikel As Range
Dim rangeLookupArtikel As Range
Dim rangeLookupMPN As Range
Dim selectedCell As String
Dim lookupCell As String
Dim rowCount As Integer
Dim i As Integer
Set rangeMpn = Worksheets("Resultsheet").Range("B2:B15")
Set rangeArtikel = Worksheets("Resultsheet").Range("C2:C15")
Set rangeLookupArtikel = Range("A2:A8")
Set rangeLookupMPN = Range("B2:B8")
rowCount = 14
For i = 2 To rowCount
selectedCell = "C" & CStr(i)
lookupCell = "B" & CStr(i)
Range(selectedCell) = Application.XLookup("*" & Range(lookupCell) & "*", Worksheets("LookupSheet").Range("A2:A9"), Worksheets("LookupSheet").Range("B2:B9"), "NOT FOUND", 2)
On Error Resume Next
If Range(selectedCell) = "" Then
Range(selectedCell) = "FOUND BUT NULL"
End If
Next i
End Sub