Matching similar but not exact text strings in Excel VBA projects

后端 未结 5 1754
忘了有多久
忘了有多久 2020-11-30 10:31

Okay, I have been trying to find a solution for this and I just don\'t seem to be able. I can\'t even break down the problem properly. This is the idea.

I have two s

5条回答
  •  臣服心动
    2020-11-30 11:20

    I would place the macro in your PERSONAL section, this way the macro is available in all worksheets. Do this by recording a dummy macro and select to store it in Personal Macro workbook. Now you can manually add new macro's and functions in this personal workbook.

    I just tried this one (don't know the original source) and it works fine.

    The formula looks like this: =PERSONAL.XLSB!FuzzyFind(A1,B$1:B$20)

    The code is here:

    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
    

提交回复
热议问题