Matching similar but not exact text strings in Excel VBA projects

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

提交回复
热议问题