VBA Fuzzy Lookup (Find) - Case Sensitive Issue

hamidxa

New Member
Joined
Oct 11, 2013
Messages
19
Hi,

I have a function that performs a "fuzzy" lookup/find against a set of data to find matching values.

For example, for each cell in Field 1, the value in that cell is looked up against the entire range of data in Field 2,
and where there is a "Fuzzy" Find/match, that value is populated on the same row in the column named "FUZZY FIND".
I have a separate function that calculates a similarity score in column D.

To illustrate this: Cell A2 = the number 123, well, 123 also exists in Field2 (B5), so on Cell C2 (FUZZY FIND column), the value 123 appears which is a 100% match.


FIELD1FIELD2FUZZY FINDSIMILARITY SCORE
123this is pretty awesome123100%
7899UPPERCASE78975%
hellohellhell80%
this is awesome123this is pretty awesome68%
ALEX789ALEX100%
uppercasekarl0%
alexALEX0%

<tbody>
</tbody>


The problem I am running into however is that my FuzzyFind function is case sensitive, whereas I would like it to ignore the case -- or perhaps consider all of the data to be upper or lower case, if that's the easier solution.

For example, in cell A6 I have the name "ALEX" appear, which also appears in Field 2 exactly as "ALEX", so in cell C6, the corresponding Fuzzy Find = "ALEX", which is rated as a 100% match.

However, in cell A8, the name "alex" appears as lower case, of which there is no lowercase variant in Column 2, so in cell C8, my Fuzzy Find value is simply blank, with a 0% match rate.

On the other hand, I would like "alex" and "ALEX" to both be treated equally, and correspond to a 100% match.

I've played around with the code in the function used to generate the values in Col C, but can't figure out a way to get it to either ignore case or normalize the case (all UPPER or lower) against both columns being evaluated.

I can't add attachments, but below is the same code I used against the same dataset in the above table that I hope someone can take a look at, and lend a helping hand.

Code:
Function FuzzyFind(lookup_value As String, tbl_array As Range) As StringDim i As Integer, str As String, Value As String
Dim a As Integer, b As Integer, cell As Variant
For Each cell In tbl_array
  str = cell
  For i = 1 To Len(lookup_value)
    If InStr(cell, Mid(lookup_value, i, 1)) > 0 Then
      a = a + 1
      cell = Mid(cell, 1, InStr(cell, Mid(lookup_value, i, 1)) - 1) & Mid(cell, InStr(cell, Mid(lookup_value, i, 1)) + 1, 9999)
    End If
  Next i
  a = a - Len(cell)
  If a > b Then
    b = a
    Value = str
  End If
  a = 0
Next cell
FuzzyFind = Value
End Function

Thanks in advance!
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Possibly...

Code:
Function FuzzyFind(lookup_value As String, tbl_array As Range) As String
    Dim i As Integer, str As String, Value As String
    Dim a As Integer, b As Integer, cell As Variant
    For Each cell In tbl_array
        str = cell
        For i = 1 To Len(lookup_value)
            If InStr(UCase(cell), Mid(UCase(lookup_value), i, 1)) > 0 Then
                a = a + 1
                cell = Mid(UCase(cell), 1, InStr(UCase(cell), Mid(UCase(lookup_value), i, 1)) - 1) & Mid(UCase(cell), InStr(UCase(cell), Mid(UCase(lookup_value), i, 1)) + 1, 9999)
            End If
        Next i
        a = a - Len(cell)
        If a > b Then
            b = a
            Value = str
        End If
        a = 0
    Next cell
    FuzzyFind = Value
End Function
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,985
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top