可以使用VBA编写代码实现此功能。以下是示例代码:
Sub CompareAndMoveData()
Dim ws1 As Worksheet Dim ws2 As Worksheet Dim ws3 As Worksheet
Dim lastrow1 As Long Dim lastrow2 As Long Dim lastrow3 As Long
Dim i As Long Dim j As Long Dim k As Long
Set ws1 = ThisWorkbook.Sheets("Sheet1") Set ws2 = ThisWorkbook.Sheets("Sheet2") Set ws3 = ThisWorkbook.Sheets("Sheet3")
lastrow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row lastrow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row lastrow3 = ws3.Cells(Rows.Count, 1).End(xlUp).Row
k = 1
For i = 1 To lastrow1 For j = 1 To lastrow2 If ws1.Cells(i, 1) = ws2.Cells(j, 1) Then ws3.Cells(k, 1) = ws1.Cells(i, 1) ws3.Cells(k, 2) = ws1.Cells(i, 2) ws3.Cells(k, 3) = ws2.Cells(j, 2) k = k + 1 End If Next j Next i
End Sub
此代码将比较Sheet1和Sheet2中的第一列数据,并将匹配的行从Sheet1的第二列和Sheet2的第二列复制到Sheet3的第一列,第二列和第三列中。最终结果将显示在第三个工作表中。