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.


[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]FIELD1[/TD]
[TD]FIELD2[/TD]
[TD]FUZZY FIND[/TD]
[TD]SIMILARITY SCORE[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]this is pretty awesome[/TD]
[TD]123[/TD]
[TD]100%[/TD]
[/TR]
[TR]
[TD]7899[/TD]
[TD]UPPERCASE[/TD]
[TD]789[/TD]
[TD]75%[/TD]
[/TR]
[TR]
[TD]hello[/TD]
[TD]hell[/TD]
[TD]hell[/TD]
[TD]80%[/TD]
[/TR]
[TR]
[TD]this is awesome[/TD]
[TD]123[/TD]
[TD]this is pretty awesome[/TD]
[TD]68%[/TD]
[/TR]
[TR]
[TD]ALEX[/TD]
[TD]789[/TD]
[TD]ALEX[/TD]
[TD]100%[/TD]
[/TR]
[TR]
[TD]uppercase[/TD]
[TD]karl[/TD]
[TD][/TD]
[TD]0%[/TD]
[/TR]
[TR]
[TD]alex[/TD]
[TD]ALEX[/TD]
[TD][/TD]
[TD]0%[/TD]
[/TR]
</tbody>[/TABLE]


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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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