比较Excel列A与列B的数据
1,列A有,列B没有
Private Sub CommandButton1_Click()
x = InputBox("请输入末行号")
For i = 1 To
x '假定首行为标题行,如果没有标题行,则将2改为1
strA = Worksheets("sheet1").Cells(i, 1).Value '得到列1的数据
For j = 1 To x '遍历列2
strB = Worksheets("Sheet1").Cells(j, 2).Value '得到列2的数据
If strB = strA Then
'如果列1中有,而列2中也有,则跳过,比较下一个列1的数据
GoTo a:
End If
Next j
Worksheets("sheet1").Cells(i, 3).Value =
Worksheets("sheet1").Cells(i, 1).Value
'列1中的某在数据在列2中不存在,则写入列3
a:
Next i
End Sub
2,列B有,列A没有
Private Sub CommandButton2_Click()
x = InputBox("请输入末行号")
For i = 1 To
x '假定首行为标题行,如果没有标题行,则将2改为1
strA = Worksheets("sheet1").Cells(i, 2).Value
For j = 1 To x
strB = Worksheets("Sheet1").Cells(j, 1).Value
If strB = strA Then
GoTo a:
End If
Next j
Worksheets("sheet1").Cells(i, 3).Value =
Worksheets("sheet1").Cells(i, 2).Value
a:
Next i
End Sub
3,列A,列B都有
Private Sub CommandButton3_Click()
x =
InputBox("请输入末行号")
For i = 1 To
x '假定首行为标题行,如果没有标题行,则将2改为1
strA = Worksheets("sheet1").Cells(i, 1).Value
For j = 1 To x
strB = Worksheets("Sheet1").Cells(j, 2).Value
If strB = strA Then
1,列A有,列B没有
Private Sub CommandButton1_Click()
a:
End Sub
2,列B有,列A没有
Private Sub CommandButton2_Click()
a:
End Sub
3,列A,列B都有
Private Sub CommandButton3_Click()

加载中…