Sub 比對()
Dim Ri&, Rn&, i&, j&, n&, Arr, Brr
Ri = Cells(Rows.Count, 1).End(3).Row
Arr = [A2].Resize(Ri - 1, 2)
Rn = Ri * (Ri - 1)
If Rn >= Rows.Count Then Rn = Rows.Count - 1
Brr = [K2].Resize(Rn, 3): n = 1
For i = 1 To Ri - 1: For j = 1 To Ri - 1
If i <> j Then
If Arr(i, 2) = Arr(j, 2) Then
Brr(n, 1) = Arr(i, 1)
Brr(n, 3) = Arr(j, 1)
n = n + 1
Else: Exit For: End If
End If
Next: Next
[K2].Resize(Rn, 3) = Brr
End Sub作者: peter631114 時間: 2019-6-14 10:22
Dear Sir
如你所了解沒有錯,我試用了你的code進行測試,結果是對的~~只是目前程式只有比對到判斷條件為0的部分,當判斷條件為"1"或2以後就沒有比對了~~作者: n7822123 時間: 2019-6-14 11:09
Sub TEST()
Dim Arr, Brr, i&, j&, N&
Arr = Range([B1], Cells(Rows.Count, 1).End(xlUp))
ReDim Brr(1 To 20000, 1 To 3)
For i = 2 To UBound(Arr)
For j = 2 To UBound(Arr)
If i <> j And Arr(i, 2) = Arr(j, 2) Then
N = N + 1
Brr(N, 1) = Arr(i, 1)
Brr(N, 3) = Arr(j, 1)
End If
Next j
Next i
If N = 0 Then Exit Sub
[F2:H2].Resize(N) = Brr
End Sub