我有两张不同的数据表。 Sheet1.A将包含一个字母数字条目“ABC123”和Sheet2.A将包含一个类似的条目“ABC123 some text”或“some text ABC123”
此外,Sheet1将始终具有比Sheet2更少的条目,因此将存在不匹配。
在Sheet3中,我希望能够显示Sheet1.A的所有条目及其与Sheet2.A的相应匹配,然后对于所有非匹配,我希望它们显示在列表的底部。
理想输出示例:
Sheet3.A Sheet3.B
ABC123 ABC123
ABC222 ABC222
ABC333 ABC333
ABC444
ABC555
ABC666
目前我正在使用Sheet3.B的索引匹配(具有LEFT功能)公式,但不会产生理想的输出:
Sheet3.A Sheet3.B
ABC123 ABC123
ABC222 ABC222
ABC333 ABC333
ABC444
ABC444
ABC444
另外因为我使用LEFT函数而且Sheet2.A中的数据可能不像Sheet1.A那样排列,所以找不到某些条目,从而产生#N / A
我还想补充一点,Sheet2.A可能包含超过256个字符,这会导致索引匹配功能出现问题。这个问题不是首要问题,但如果能够解决这个问题,那就太棒了。
修改 的
问题和接受的答案现在正确反映了彼此
答案 0 :(得分:1)
您可以使用.Find
方法搜索部分匹配。
Sub FindPartialString()
Dim wsList As Worksheet
Dim wsSearch As Worksheet
Dim wsOutput As Worksheet
Dim lastRow As Long
Dim rngList As Range
Dim rngMatch As Range
Dim cl As Range
Dim arrNonMatches() As Variant
Dim nonMatchCount As Long
Set wsList = Sheets(1) '## Modify as needed
Set wsSearch = Sheets(2) '## Modify as needed
Set wsOutput = Sheets(3) '## Modify as needed
Set rngList = wsList.Range("A2:A5") '## Modify as needed
For Each cl In rngList
Set rngMatch = Nothing 'clear the container before each query
'look for a partial match:
Set rngMatch = wsSearch.Cells.Find(What:=cl.Value, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'Store the matches and non matches in separate arrays:
If Not rngMatch Is Nothing Then
lastRow = 1 + Application.WorksheetFunction.CountA(wsOutput.Range("A:A"))
'put the searched value in column A:
wsOutput.Cells(lastRow, 1) = cl.Value
'Put the found value in column B:
wsOutput.Cells(lastRow, 2) = rngMatch.Value
Else:
'store non-matches in an array
ReDim Preserve arrNonMatches(nonMatchCount)
arrNonMatches(nonMatchCount) = cl.Value
nonMatchCount = nonMatchCount + 1
End If
Next
'Print out the non-matches
lastRow = lastRow + 1
wsOutput.Cells(lastRow, 1).Resize(UBound(arrNonMatches) + 1, 1).Value = Application.Transpose(arrNonMatches)
End Sub