Excel中两列部分相同的名字怎么进行对比并且清晰的显示出来

2021-07-04 教育 340阅读
这个要求只能用宏来实现了,试试我写的这个:
执行完成后,会用不同颜色区分两边有差异的数据,并会在当中的空列中自动填写1或2或空,你可以用自动筛选来轻松找出差异:填写1(或2)表示第1(或2)组中此数据在第2(或1)组中不存在;空白的表示此行数据两边都能对应上。
Option Explicit
Dim x1, y1, x2, y2, x3, i, k As Integer
Sub 查找两列不同数据()
'
' 查找两列不同数据 Macro
' JamesChen 记录的宏 2008/7/18
' 使用说明:把要对比的两组数据(数据中不可有空列)放在同一个表中,中间用一个空列分隔。要比较的关键列放在两组数据的第一列上,并对两组数据按关健列从小到大排序
' 注:第一列不进行比较,作为标题。
'
' 确定第一列的列数,第一个不为空的列
' 第一行不算,从第二行开始

If MsgBox("确认要进行处理吗?" + Chr(13) + Chr(13) + "注意:" + Chr(13) + "1)各组数据必须从小到大排序;" _
+ Chr(13) + "2)第一行为标题,各组数据第二行一定要整齐不可有空格;", vbOKCancel + vbQuestion, "查找两列不同数据宏 -James") = vbCancel Then
Exit Sub
End If

x1 = 1
While Cells(2, x1) = ""
x1 = x1 + 1 '第一列的起列
Wend
' 确定第二列的列数
'先找到第一个系列的单元格
x2 = x1 + 1
While Cells(2, x2) <> ""
x2 = x2 + 1 '第二列的起列,x2-2为第一列的止列
Wend
x2 = x2 + 1
x3 = x2 + 1
While Cells(2, x3) <> ""
x3 = x3 + 1 'x3-1为第二列的止列
Wend
' 开始比较
' k = 0
i = 2
Do
If Cells(i, x1) < Cells(i, x2) Then
Range(Cells(i, x2), Cells(i, x3 - 1)).Select
Selection.Insert Shift:=xlDown
Selection.Interior.ColorIndex = xlNone
Range(Cells(i, x1), Cells(i, x2 - 2)).Interior.ColorIndex = 6
Cells(i, x2 - 1) = 1
ElseIf Cells(i, x1) > Cells(i, x2) Then
Range(Cells(i, x1), Cells(i, x2 - 2)).Select
Selection.Insert Shift:=xlDown
Selection.Interior.ColorIndex = xlNone
Range(Cells(i, x2), Cells(i, x3 - 1)).Interior.ColorIndex = 8
Cells(i, x2 - 1) = 2
End If
i = i + 1
If Cells(i, x1) = "" Or Cells(i, x2) = "" Then Exit Do
Loop
While Len(Cells(i, x1).Text) <> 0 Or Len(Cells(i, x2).Text) <> 0
If Len(Cells(i, x2).Text) = 0 Then
Range(Cells(i, x1), Cells(i, x3 - 1)).Select
' Selection.Insert Shift:=xlDown
Selection.Interior.ColorIndex = xlNone
Range(Cells(i, x1), Cells(i, x2 - 2)).Interior.ColorIndex = 6
Cells(i, x2 - 1) = 1
Else
Range(Cells(i, x1), Cells(i, x2 - 2)).Select
' Selection.Insert Shift:=xlDown
Selection.Interior.ColorIndex = xlNone
Range(Cells(i, x2), Cells(i, x3 - 1)).Interior.ColorIndex = 8
Cells(i, x2 - 1) = 2
End If
i = i + 1
Wend
Cells(2, 1).Select
End Sub
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com