FuzzyMatch Function/Logic

svkroy

Board Regular
Joined
Sep 12, 2009
Messages
179
A simple fuzzymatch logic that can be placed in an access module. This is a UDF(User Defined Function), and can be called from a query to retrieve suitable results.

Variations can involve replacing special characters, vowels before matching so as to customise as per requirements.

Code:
Function FuzzyMatch_Souvik(ByVal String1 As String, ByVal String2 As String)


Dim intLen1 As Integer, intLen2 As Integer ', matchpercent As Integer


intLen1 = Len(String1)
intLen2 = Len(String2)


'Other amendments
String1 = LCase$(Trim(String1))
String2 = LCase$(Trim(String2))


FuzzyMatch_Souvik = ""


'Extreme Cases
If String1 = String2 Then
    FuzzyMatch_Souvik = 1 ' * 100
    'Exit Function
End If


If intLen1 < 2 Then
    FuzzyMatch_Souvik = 0
    'Exit Function
End If


If Not IsNull(FuzzyMatch_Souvik) Then


'Count spaces in strings
lengthstr1 = Len(String1) - Len(Replace(String1, " ", ""))
lengthstr2 = Len(String2) - Len(Replace(String2, " ", ""))


'Split strings into different words
For i = 0 To lengthstr1
String1Array = Split(String1)
Next i


For i = 0 To lengthstr2
String2Array = Split(String2)
Next i


'Match String1 with String2
For i = LBound(String1Array) To UBound(String1Array)
    dup = False
   For j = LBound(String2Array) To UBound(String2Array)
        If String1Array(i) = String2Array(j) Then
        matchquotient = matchquotient + 1
        matchpercent = matchquotient / (lengthstr1 + 1)
        
            dup = True: Exit For
        End If
    Next j
    If Not dup Then
        remainder = remainder + String1Array(i)
    End If
Next i


'Match String2 with String1
For i = LBound(String2Array) To UBound(String2Array)
    dup = False
   For j = LBound(String1Array) To UBound(String1Array)
        If String2Array(i) = String1Array(j) Then
        matchquotient1 = matchquotient1 + 1
        matchpercent1 = matchquotient1 / (lengthstr2 + 1)
        
            dup = True: Exit For
        End If
    Next j
    If Not dup Then
        remainder = remainder + String2Array(i)
    End If
Next i


End If

FuzzyMatch_Souvik = (matchpercent + matchpercent1) / 2


End Function
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Thank you for this logic...Not sure if you are able to assist.
I have a single table with my string - how can I modify the code to go to row 1 - run through the rest of row 1 and see if there are matches - write possible matches to a new table, and then move to next record and got to row 2 and see if there is a match
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

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