比较匹配和非匹配的两个数据集

我有两个不同的表中的数据。 Sheet1.A将包含字母数字条目“ABC123”,Sheet2.A将包含类似条目“ABC123一些文本”或“一些文本ABC123”

另外,Sheet1的入口总是less于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那样排列,所以有些条目没有find,从而生成#N / A

我还想补充说Sheet2.A可能包含超过256个字符,这会导致索引匹配function的问题。 这个问题不是重中之重,但是如果可以解决的话,那也不错。

编辑:

问题和接受的答案现在正确地反映彼此

您可以使用.Find方法,search部分匹配。

 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