View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Patrick Molloy Patrick Molloy is offline
external usenet poster
 
Posts: 1,049
Default Similar Function in VBA - does it exist ?

there's the LIKE method ... maybe you should replace spaces with asterisks


Sub test()
Select Case similar("Aston Martin", "Asto Martin ")
Case 0
MsgBox "No Match"
Case 1
MsgBox "Exact Match"
Case 2
MsgBox "Similar"
Case Else
End Select
End Sub

Function similar(text1 As String, text2 As String) As Long
text1 = Replace(text1, " ", "*")
text2 = Replace(text2, " ", "*")
If text1 = text2 Then
similar = 1
ElseIf text1 Like text2 Then
similar = 2
End If


End Function


I noticed that this is exactly the same as Tim's response...hmmm
HOWEVER what it does is to show you that a UDF could be used. You need to
think of an algorithm, a rule if you will, that can be coded to define how
the matching works. what is a "partial" match? The human brain recognizes
patterns awesomely fast, but to do this with a computer means a huge amount
of coding - and probably outside the scope of your project.



"christopher ward" wrote in
message ...
Dear Experts

I have a need to compare 2 data items - Var1 and Var2 which are both
strings

Var1 may have "Aston Martin" within it and Var2 may have "Asto Martin" -
i.e
they are similar and a user has spelt Var2 incorrectly.

I need to evaluate them so I can see in a long list they are the same -
has
VBA a function which compares Is Similar or has any one an idea how this
may
be done

As always I appreciate your ideas and thoughts and thank you in advance

Chris
--
C Ward