0

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

  1. 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.

Looking for MPN values on another sheet The LookupSheet. Just as the picture above the datatype of the numeric and other Values are General. LookupSheet

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
DanielvL
  • 30
  • 5
  • 1
    Since you do not show your data, your results, the formula you are using on the worksheet, etc, I can only speculate that there is a data type mismatch between `lookup_value` and `lookup_array` – Ron Rosenfeld Jul 17 '23 at 10:13
  • Hi Ron thanks for your comment. As stated I tried storing the cell data as General, Number and Text. This did not change the outcome. – DanielvL Jul 17 '23 at 10:17
  • How did you do that, ***exactly***? – Ron Rosenfeld Jul 17 '23 at 10:21
  • I changed the number format of the cells using the dropbdown in: Home ribbon>Number. – DanielvL Jul 17 '23 at 11:02
  • 3
    Changing the number format does **NOT** change the data type. You can prove this to yourself by applying the formula `=istext(cell_ref)` or `isnumber(cell_ref)` and observing the results of merely changing the number format. You must also re-enter or update the value. – Ron Rosenfeld Jul 17 '23 at 11:07
  • Thank you Ron, I have tried but the when I change the number format, the outcome of the formulas you stated do change. – DanielvL Jul 17 '23 at 11:31
  • That's odd. They don't change here, and I don't have any problem with XLOOKUP either on the worksheet or in VBA, provided the data types match. Somthing in your environment is causing the problem, but you have not shared sufficient information for me to reproduce your problem. – Ron Rosenfeld Jul 17 '23 at 12:21
  • Hi Ron, I have replicated my issue and added it to the question description. Do you have any suggestions? – DanielvL Jul 17 '23 at 13:31
  • If MPN contains a mixture of numeric and non-numeric values, you should probably make sure all the ones that look like numbers are "numbers stored as text" instead of actual numeric values (prepend a single quote for example) Then you'll be able to treat them as if they were text, and your wildcard lookups will succeed. – Tim Williams Jul 17 '23 at 16:28
  • Hi Tim, This solved my issue. I can loop through the the selected cell and lookup array to prepend a " ' ". – DanielvL Jul 18 '23 at 08:36
  • There is one issue left, when I try to add a single quote to the numeric values in VBA, it only does so for the non numeric only values. – DanielvL Jul 18 '23 at 09:00

1 Answers1

3

Given your data, it is not apparent to me why you need wild card lookups.

Also XLOOKUP can return an array, so there is no need to process each cell individually.

In general, VBA routines run faster if one minimizes the access to the worksheet cells, and does the processing solely in VBA. In this particular instance, since XLOOKUP will return an array, I'm not certain which method would be faster, but I have shown the "process in VBA" routine.

For a non-wild card XLOOKUP you could adapt the following:

Option Explicit
Sub due()
    Dim vLookupVal As Variant
    Dim vLookupArr As Variant
    Dim vLookupRes As Variant
    Dim vArtNum As Variant
    Dim v As Variant
    Dim rArtNum As Range
    
With ThisWorkbook.Worksheets("Sheet1")
    vLookupVal = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    vLookupArr = Range(.Cells(1, 8), .Cells(.Rows.Count, 8).End(xlUp))
    vLookupRes = Range(.Cells(1, 9), .Cells(.Rows.Count, 9).End(xlUp))
    Set rArtNum = .Cells(1, 2).Resize(rowsize:=UBound(vLookupVal))
End With

     rArtNum = Application.XLookup(vLookupVal, vLookupArr, vLookupRes, "NOT FOUND")
    
End Sub

enter image description here

If you require the wildcard for some reason not apparent in your example, then modify

  • the vLookupVal array to be text and include the wildcards
  • the vLookupArr array to be text
  • the XLOOKUP function to do a wild card search
Option Explicit
Sub due()
    Dim vLookupVal As Variant
    Dim vLookupArr As Variant
    Dim vLookupRes As Variant
    Dim vArtNum As Variant
    Dim I As Long
    Dim rArtNum As Range
    
With ThisWorkbook.Worksheets("Sheet1")
    vLookupVal = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    vLookupArr = Range(.Cells(1, 8), .Cells(.Rows.Count, 8).End(xlUp))
    vLookupRes = Range(.Cells(1, 9), .Cells(.Rows.Count, 9).End(xlUp))
    Set rArtNum = .Cells(1, 2).Resize(rowsize:=UBound(vLookupVal))
End With

    For I = 1 To UBound(vLookupVal, 1)
        vLookupVal(I, 1) = "*" & CStr(vLookupVal(I, 1)) & "*"
        vLookupArr(I, 1) = CStr(vLookupArr(I, 1))
    Next I

     rArtNum = Application.XLookup(vLookupVal, vLookupArr, vLookupRes, "NOT FOUND", 2)
    
End Sub
Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
  • How would you apply your code if the lookup table is on another sheet? – DanielvL Jul 18 '23 at 07:36
  • @DanielvL Just change the worksheet reference to that other sheet for the array from that table. – Ron Rosenfeld Jul 18 '23 at 10:12
  • Rosenfield the first code snippet you gave me works well, but I can't figure out how to add wildcard values to the code. I tried adding the asterix (*) symbol around the Looklup Value and converting the Lookup array and the Lookup Value to Strings using the Cstr() function. I also tried using ChatGPT to fix the bug, it can't find a solution. Any suggestions? – DanielvL Jul 19 '23 at 10:13
  • @DanielvL Use the second snippet. – Ron Rosenfeld Jul 19 '23 at 10:31
  • Rosenfield Would it also be possible to match the following values using this wild card solution: 171960501AB with 171960501? – DanielvL Jul 19 '23 at 11:39
  • Yes, if the latter is the lookup value and the former is located in the lookup array. – Ron Rosenfeld Jul 19 '23 at 11:51
  • The first value is the Lookup Value (171960501AB ) and the latter is a value in the Lookuparray(171960501). It does not match with the current code. – DanielvL Jul 19 '23 at 11:54
  • @DanielvL Wild cards do not work that way. You will need a different algorithm depending on your data. For example, if the numeric portion is the constant, you will need to split look up value so you are only examining in numeric portion. – Ron Rosenfeld Jul 19 '23 at 12:21
  • The problem is that it can only find wildcard values if there is extra text at the start or the end of the value in the Lookuparray. I will accept the answer as a solution, but there is still room for improvement. – DanielvL Jul 26 '23 at 11:31
  • @DanielvL That situation is not apparent in the data sample you provided, and, since wild cards do not "work in reverse", the solution will not work for that sort of issue. As I commented above, the data will determine a viable solution. – Ron Rosenfeld Jul 26 '23 at 20:56