Matching similar but not exact text strings in Excel VBA projects

后端 未结 5 1734
忘了有多久
忘了有多久 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:14

    I used Robert solution and it works fine for me. I am posting whole solution for people who are new for excel but knows coding:

    Though this thread is old but I took some code from another threads and tried and looks like solution is giving approx match. Here I am trying to match one column of sheet1 with one column of sheet2:

    1. add command button in excel
    2. put following code and click/run button and function gives you result in selected column
     Private Sub CommandButton21_Click()
         Dim ws As Worksheet
         Dim LRow As Long, i As Long, lval As String
    
    
       '~~> Change this to the relevant worsheet
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
    With ws
        '~~> Find Last Row in Col G which has data
        LRow = .Range("D" & .Rows.Count).End(xlUp).Row
    
        If LRow = 1 Then
            MsgBox "No data in column D"
        Else
            For i = 2 To LRow
    
    
                 lval = "D"
                .Range("G" & i).Value = FuzzyFind(lval & i, .Range("PWC"))
            Next i
        End If
        End With
    
        End Sub
    
    
        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
          If Value <> "" Then
             FuzzyFind = Value
          Else
             FuzzyFind = "None"
          End If
    End Function
    
    0 讨论(0)
  • 2020-11-30 11:18

    Not exactly on point but similar, and people dealing with my issue are likely to find this page when searching.

    Task: A list of patients who have been in car wrecks, including street addresses. Find related accounts based on same street address. The list will be a maximum of maybe 120 records--so partial manual review is realistic.

    Problem: Street addresses are similar but not identical, e.g. 123 JONES LANE and 123 JONES LN or 72 MAIN STREET #32 and 72 MAIN STREET # 32.

    Part of the solution is to compare only the street numbers. With a list that size it's unusual to have two different addresses with the same street number (e.g., 123 JONES LANE and 123 MAIN STREET).

    Caution: You can't use VAL() to pull the street number. Try it with 167 E 13 ST. VBA sees that as 167^13 and will crash if you are outputting to Street_Num As Integer. So you have to use a loop to pull the digits into a new string and stop at the first non-digit character.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 11:24

    You can Google Excel UDF Fuzzy lookup or Levensthein distance. There are some UDF's floating around and Microsoft does have a Fuzzy lookup/match add-on as well (when I used it, it was crash prone and not intuitive).

    0 讨论(0)
  • 2020-11-30 11:37

    Take a look at the functions on this DDoE post. You could generate a longest common sequence string and compare the length to the original string. Feed it some known matches and some close non-matches and see if you can see a clear dividing line between them.

    These functions are used for diffing, not finding close matches, but they may work for you.

    0 讨论(0)
提交回复
热议问题