我有2张excel,10列但数据不同。我想比较两张纸,并根据4个特定列的值突出显示差异。例如,
表1:
id name address pincode
1 AA XXX 123
表2:
id name address pincode
2 BB YYY 345
1 AA XXX 123
在比较上述数据时,我希望两张纸中的公共行用颜色突出显示。
我怎样才能在Excel中执行此操作?
干杯
答案 0 :(得分:0)
要检查2行是否相等,您可以使用:
If Join(Application.Transpose(Application.Transpose(Sheets("sheet1").Rows(3).Value)), Chr(0)) = _
Join(Application.Transpose(Application.Transpose(Sheets("sheet2").Rows(4).Value)), Chr(0)) Then
Sheets("sheet1").Rows(3).Interior.ColorIndex = 3
Sheets("sheet2").Rows(4).Interior.ColorIndex = 3
End If
您必须使用工作表名称替换“sheet1”和“sheet2”并调整行。如果行彼此相等,则它们将以红色突出显示。
如果您使用此功能,则必须将第一张纸的每一行与第二张纸的每一行进行比较,这可能会非常慢
示例:
Dim usedRange1 As Integer
Dim usedRange2 As Integer
usedRange1 = Sheets("sheet1").UsedRange.SpecialCells(xlCellTypeLastCell).Row
usedRange2 = Sheets("sheet2").UsedRange.SpecialCells(xlCellTypeLastCell).Row
Dim i As Integer, j As Integer
Application.ScreenUpdating = False
For i = 1 To usedRange1 Step 1
'Check if row in sheet1 is not empty
If Application.CountA(Sheets("sheet1").Rows(i)) <> 0 Then
For j = 1 To usedRange2 Step 1
'check if row in sheet2 is not empty
If Application.CountA(Sheets("sheet2").Rows(j)) <> 0 Then
If Join(Application.Transpose(Application.Transpose(Sheets("sheet1").Rows(i).Value)), Chr(0)) = _
Join(Application.Transpose(Application.Transpose(Sheets("sheet2").Rows(j).Value)), Chr(0)) Then
Sheets("sheet1").Rows(i).Interior.ColorIndex = 3
Sheets("sheet2").Rows(j).Interior.ColorIndex = 3
End If
End If
Next j
End If
Next i
Application.ScreenUpdating = True