我使用了罗伯特解决方案,它对我来说很好。我正在为那些不熟悉 excel 但知道编码的人发布完整的解决方案:
虽然这个线程很旧,但我从另一个线程中获取了一些代码并尝试过,看起来解决方案给出了近似匹配。在这里,我试图将 sheet1 的一列与 sheet2 的一列匹配:
- 在excel中添加命令按钮
- 输入以下代码并单击/运行按钮和函数为您提供选定列的结果
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