Sub CompareAndHighlightUsingDictionary()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim i As Long
Dim key As Variant
Dim dict As Object
' 设置工作表
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' 获取两个工作表中最后一行的行号
lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
' 创建字典对象
Set dict = CreateObject("Scripting.Dictionary")
' 将表二的A列和B列数据存入字典
For i = 1 To lastRow2
key = ws2.Cells(i, 1).Value & "|" & ws2.Cells(i, 2).Value
dict(key) = i
Next i
' 遍历表一的数据,进行比较并标记
For i = 1 To lastRow1
key = ws1.Cells(i, 1).Value & "|" & ws1.Cells(i, 2).Value
If dict.exists(key) Then
' 标记表一和表二的相同行为红色
ws1.Rows(i).
网页链接 = RGB(255, 0, 0)
ws2.Rows(dict(key)).
网页链接 = RGB(255, 0, 0)
End If
Next i
End Sub