模糊字符串匹配 Excel

时间:2021-01-14 09:03:07

标签: excel vba fuzzy-search

我目前需要一种模糊字符串匹配算法。我从这里给出的链接中找到了一个 VBA 代码:Fuzzy Matching

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(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

然而,无论“正确答案”有多远,这都能为您提供匹配。有什么方法可以实现函数给出“N/A”,比如说,距离原始字符串有 4 个字符或更多?

0 个答案:

没有答案
相关问题