Excel表格中如何将AB两列进行对比找到重复项,把A列中找到的重复项同行的B列数据放到B列重复项后的C列中?
假设我的表名“区划”,在EFG三列中显示整理后的数据
右键表名【区划】,点击查看代码
输入以下代码:
Sub 区划整理()
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim n As Integer
n = Application.CountA(Range("A:A"))
i = 1
x = 1
Do While i <= n
If Cells(i, 1) = "中国" Then
Cells(x, 5) = Cells(i, 1)
Cells(x, 6) = Cells(i, 2)
j = 1
Do While j <= n
If Cells(j, 1) = Cells(i, 2) Then
Cells(x, 7) = Cells(j, 2)
x = x + 1
End If
j = j + 1
Loop
End If
i = i + 1
Loop
End Sub
3.保存,会提示是否启用宏功能,选“是”
4.【视图】——【宏】——【查看宏】,选择刚才编写的宏【区划整理】,点击执行。
这个如果最上层的不只是中国怎么代码要怎么编呢
看你具体数据。