Public Function Soundex(strName As String) As String
Dim strCode As String, strCodeN As String
' Length of original string, last code returned, looping integer
Dim intLength As Integer, strLastCode As String, intI As Integer
' Save the first letter
strCode = UCase(Left(strName, 1))
' Save its code number to check for duplicates
strLastCode = GetSoundexCode(strCode)
' Calculate length to examine
intLength = Len(strName)
' Create the code starting at the second letter.
For intI = 2 To intLength
strCodeN = GetSoundexCode(Mid(strName, intI, 1))
' If two letters that are the same are next to each other
' only count one of them
If strCodeN > "0" And strLastCode <> strCodeN Then
' Different code number, add to the result
strCode = strCode & strCodeN
End If
' If this is not the special "skip" code (H or W)
If strCodeN <> "0" Then
' Save the last code number
strLastCode = strCodeN
End If
' Loop
Next intI
' Check the length
If Len(strCode) < 4 Then
' Pad zeros
strCode = strCode & String(4 - Len(strCode), "0")
Else
' Make sure not more than 4
strCode = Left(strCode, 4)
End If
' Return the result
Soundex = strCode
End Function
Function GetSoundexCode(strCharString) As String
Select Case strChar
Case "B", "F", "P", "V", "Β", "Φ", "Π" '3 last letters is Greek Characters
GetSoundexCode = "1"
Case "Ψ" 'is Greek Character
GetSoundexCode = "12"
Case "C", "G", "J", "K", "Q", "S", "X", "Z", "Γ", "Ζ", "Κ", "Ξ", "Σ", "Χ" '6 last letters is Greek Characters
GetSoundexCode = "2"
Case "D", "T", "Δ", "Θ", "Τ"
GetSoundexCode = "3"
Case "L", "Λ"
GetSoundexCode = "4"
Case "M", "N", "Μ", "Ν"
GetSoundexCode = "5"
Case "R", "Ρ"
GetSoundexCode = "6"
Case "H", "W", "Α", "Ε", "Ι", "Ο", "Υ", "Ω", "Η"
' Special "skip" code
GetSoundexCode = "0"
End Select
End Function