我要比较2个清单。清单1填入栏A和B,清单2填入栏D和E。
我希望列表1和列表2中的共同项目以黄色突出显示。
如果某个项目不在列表1中,但在列表2中,我希望将其复制、粘贴到表2中。
如果一个项目出现在列表1中,但没有出现在列表2中,我希望他们复制,粘贴到工作表3。
我写了下面的代码,有着前面提到的目标。但是它没有给出预期的输出。有人能帮助我吗?请...
输入enter image description here
输出:enter image description here
工作表2 enter image description here
工作表3 enter image description here
Sub CompareTwoColumns()
Dim compare1 As Variant, Compare2 As Variant
Dim r As Integer, q As Integer, dif1 As Integer, dif2 As Integer, m As Integer, n As Integer
Dim lr1 As Integer, lr2 As Integer
lr1 = Range("a1").End(xlDown).Offset(-1, 0).Row
lr2 = Range("d1").End(xlDown).Offset(-1, 0).Row
For r = 3 To lr1
Set compare1 = Cells(r, 1)
For q = 3 To lr2
Set Compare2 = Cells(q, 4)
If compare1 = Compare2 Then GoTo z:
Next q
Range(Cells(r, 1), Cells(r, 1).End(xlToRight)).Copy
dif1 = dif1 + 1
Sheets(2).Cells(dif1, 1).PasteSpecial Paste:=xlPasteValues
z:
Cells(q, 4).Interior.Color = vbYellow
Next r
For m = 3 To lr2
Set Compare2 = Cells(m, 4)
For n = 3 To lr1
Set compare1 = Cells(n, 1)
If Compare2 = compare1 Then GoTo y:
Next n
Range(Cells(n, 4), Cells(n, 4).End(xlToRight)).Copy
dif3 = dif3 + 1
Sheets(3).Cells(dif3, 1).PasteSpecial Paste:=xlPasteValues
y:
Cells(n, 1).Interior.Color = vbYellow
Next m
End Sub
1条答案
按热度按时间iswrvxsc1#
If I understand you correctly ....
Before running the macro, the condition of the worksheets is something like this :
In Sheet DATA, there are two tables. First table range is in column A to B until whatever row, the second table range is in column D to E until whatever row. In the example of the image above, the last row with data of both tables ended in row 7.
The data in each column of both tables are not unique. As in the image in List1, there is "aaa" value which appear two times and in List2 there is "bbb" value which appear two times.
I want common items in List 1 and List 2 to be highlighted in yellow.
if there is value in column A which found in column D (and vice versa), the cell of this value will have yellow background color. So in the sample image, the expected result in List1 cell A3:B3, A4:B4, A5:B5 ... will be yellow and in List2 cell D2:E2, D3:E3, D5:E5, D6:E6 will be yellow.
If an item is present in List 1, but not present in List 2, I want them copy, pasted in sheet 3.
cell A1:B1, A6:B6 and A7:B7 will be copied to Sheet "NotInList2"
If an item is not present in List 1, but present in List 2, I want them copy, pasted in sheet 2.
cell D4:E4 and D7:E7 will be copied to Sheet "NotInList1"
So, the result will be something like the image below :
If your case is similar with the example above ...
Basically, the code loop two times to check List1 against List2 in the first loop, then check List2 againts List1 in the second loop.
In each of the outer loop, it create a unique value to check as arr variable.
Then it loop to each element in arr to get the cell address of the looped element, then it check if the looped element in arr is found in another List, then it put yellow color. If it doesn't, then it copy to the designated sheet.
Please note, the code compares what is in column A against what is in column D (and vice versa). It doesn't compare what is in column B against what is in column E (and vice versa).