VBA EXCELA ,B两列比对
两列的顺序不一样.想查出A有或B没有的.或A没有B有的.
用VLOOK UP,效果不是很好.在这求高手用VBA帮帮我吧.
注意:A列的数字都是10位数,B列的数字有的是10位,有的是12位.
A B
1557560000 4155000200
6290108000 6290108000-1
6290108001 1157000000
4709807000 4709807000-3
4709807001 7852621000
4709807002
4709807003
6290108000=6290108000-1
6290108001=6290108000-1
4709807003=4709807000-3
这些都相等.
因为是两个不同的表.找出A列有B无,或B列有A无的.
这个就要RUN 出A列1557560000(B列无),
B列7852621000(A列无)
接http://zhidao.baidu.com/question/274558988.html
6290108000-1,代表两个数字,6290108000和6290108001
为了方便统计,就简写为6290108000-1
上面的程序RUN出来后A列的6290108001还是填充成了黄色的. 展开
上午已经回答了呀
Sub Macro1()
Dim k As Integer, bln1 As Boolean
Dim i As Long, j As Long
Columns("A:B").Interior.ColorIndex = xlNone
For i = 2 To 65000
If Cells(i, 1) = "" Then Exit For
bln1 = False
For j = 2 To 65000
If Cells(j, 2) = "" Then
'在B列没有找到i行A列的数据
If Not bln1 Then
With Cells(i, 1).Interior
.ColorIndex = 6 '填充黄色
.Pattern = xlSolid
End With
End If
Exit For
Else
If InStr(1, Cells(j, 2), Cells(i, 1), vbTextCompare) > 0 Or _
CStr(Cells(i, 1)) = Left(Cells(j, 2), Len(Cells(j, 2)) - 3) _
& Right(Cells(j, 2), 1) Then
bln1 = True '在B列找到i行A列的数据
With Cells(j, 2).Interior
.ColorIndex = 3 '填充红色,
.Pattern = xlSolid
End With
End If
End If
Next j
Next i
MsgBox "A列填充黄色代表在B列没有找到,B列没有填充颜色代表在A列没有找到!"
End Sub
我看到了,谢谢你。我希望上面两个黄色的47098007001和4709807002不要填充成黄色。
因为4709807000 ,4709807001,4709807002,4709807003都等于479807000-3
因为相等,A列47098007001,2在B列找到了(就是4709807000-3),就不能再填充成黄色了。
'B列有4709807000-3,如果A列没有4709807003,而只有4709807001,那么4709807000-3=4709807001么?
'以下程序按相等考虑
Sub Macro1()
Dim k As Integer, bln1 As Boolean
Dim i As Long, j As Long
Columns("A:B").Interior.ColorIndex = xlNone
For i = 2 To 65000
If Cells(i, 1) = "" Then Exit For
bln1 = False
For j = 2 To 65000
If Cells(j, 2) = "" Then
'在B列没有找到i行A列的数据
If Not bln1 Then
With Cells(i, 1).Interior
.ColorIndex = 6 '填充黄色
.Pattern = xlSolid
End With
End If
Exit For
Else
If Cells(j, 2) = Cells(i, 1) Then
bln1 = True '相等,在B列找到i行A列的数据
ElseIf Len(Cells(j, 2)) > Len(Cells(i, 1)) Then
If Left(Cells(i, 1), Len(Cells(i, 1)) - 1) = Left(Cells(j, 2), _
Len(Cells(i, 1)) - 1) Then bln1 = True
'4709807000-3=4709807001也算
End If
End If
If bln1 Then Exit For
Next j
If bln1 Then
'在B列找到i行A列的数据
With Cells(j, 2).Interior
.ColorIndex = 3 '填充红色,
.Pattern = xlSolid
End With
End If
Next i
MsgBox "A列填充黄色代表在B列没有找到,B列没有填充颜色代表在A列没有找到!"
End Sub