[英]compare two set of columns in two excel worksheets in same workbook
Sheet1 工作表1
+------+-------+
| ID | Name |
+------+-------+
| 1245 | James |
| 9377 | Jacob |
| 6201 | David |
| . | . |
| . | . |
| . | . |
+------+-------+
Sheet2 工作表2
+------+-------+
| ID | Name |
+------+-------+
| 1245 | James |
| 9007 | Adam |
| 9377 | Jacob |
| 6201 | David |
| . | . |
| . | . |
| . | . |
+------+-------+
In theory, both sheets are carbon copy of each other with the exception that Sheet2 have additional data. 从理论上讲,两个工作表都是彼此的复本,但Sheet2具有其他数据。 Sheet1 contains 10000 rows and Sheet2 contains 15000. How do I do a comparison between both worksheets to show/highlight the 5000 distinct rows in Sheet2? Sheet1包含10000行,Sheet2包含15000行。如何在两个工作表之间进行比较以显示/突出显示Sheet2中的5000个不同的行?
For tasks like this I use a adodb recordset to store what was on the first sheet. 对于此类任务,我使用adodb记录集来存储第一张纸上的内容。 Then i read up the second sheet and look to see if it was on the first. 然后,我阅读了第二张纸,看是否在第一张纸上。
In you VBA IDE go to the tools menu and select references. 在您的VBA IDE中,转到工具菜单,然后选择引用。 Select "Microsoft ActiveX Data Objects 2.8 Library." 选择“ Microsoft ActiveX数据对象2.8库”。
Dim rs As New ADODB.Recordset
Dim ws As Excel.Worksheet
Dim lRow As Long
Set ws = Application.ActiveSheet
'Add fields to your recordset for storing data. You can store sums here.
With rs
.Fields.Append "Row", adInteger
.Fields.Append "ID", adInteger
.Fields.Append "NAME", adChar, 50
.Open
End With
lRow = 1
'Loop through and record what is in the first sheet
Do While lRow <= ws.UsedRange.Rows.count
rs.AddNew
rs.Fields("Row").Value = lRow
rs.Fields("ID").Value = ws.Range("A" & lRow).Value
rs.Fields("NAME").Value = ws.Range("B" & lRow).Value
rs.Update
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
'Switch to the second worksheet
Set ws = Nothing
Set ws = ActiveWorkbook.Sheets("Sheet2")
ws.Activate
'Loop through and compare to what was on the first column
lRow = 1
Do While lRow <= ws.UsedRange.Rows.count
rs.Filter = ""
rs.Filter = "ID=" & ws.Range("A" & lRow).Value
If rs.RecordCount = 0 Then
'We don't have a match, color the row
Rows(lRow).Interior.Color = vbBlue
End If
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
You can just use the following function on Sheet2
to lookup Sheet2
's values and see if they exist on Sheet1
: 您可以在Sheet2
上使用以下函数来查找Sheet2
的值,并查看它们是否存在于Sheet1
:
=IF(COUNTIFS(A2,Sheet1!A:A,B2,Sheet1!B:B)>0,"","X")
This is assuming you enter the formula in C2
on Sheet2
. 假设您在Sheet2
C2
输入公式。 COUNTIFS()
just checks Sheet1
for the ID
and Name
and returns the number of rows found that match. COUNTIFS()
仅检查Sheet1
的ID
和Name
并返回找到的匹配行数。 If we find it, we don't care ( ""
). 如果找到它,我们将不在乎( ""
)。 If we don't find it, then it's exclusive to Sheet2
and we'll mark it with an "X"
. 如果找不到,则它是Sheet2
专用的,我们将其标记为"X"
。
Of course, you can do this in VBA as well: 当然,您也可以在VBA中执行此操作:
With Sheet2.Range("C2:C" & Sheet2.UsedRange.Rows.Count)
.Formula = "=IF(COUNTIFS(A2,Sheet1!A:A,B2,Sheet1!B:B)>0,"""",""X"")"
.Value = .Value ' (If you want to remove the formula)
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.