只用数组! Sub hz() Dim i As Integer, p As Integer, j As Integer, m As Integer, n As Integer Dim s As Integer, n1 As Integer, tt As Integer Dim over() As Boolean, over1() As Boolean, over2() As Boolean, over3() As Boolean Dim list() As String, list1() As String, result() As String Dim sort() As String, sort1() As String, src As String, des As String Dim arr, temp Dim pp As Integer, s1 As Integer With Worksheets("数据") p = .Range("a65536").End(xlUp).Row arr = .Range("a2:c" & p) End With m = UBound(arr, 1) ReDim over(1 To m) As Boolean ReDim over1(1 To m) As Boolean ReDim over2(1 To m) As Boolean ReDim list(1 To m, 1 To 4) As String ReDim list1(1 To m, 1 To 5) As String ReDim result(1 To m, 1 To 5) As String p = 0 For i = 1 To m s = 0 If over(i) = False Then p = p + 1 s = s + 1 list(p, 1) = arr(i, 1) list(p, 2) = arr(i, 2) list(p, 3) = arr(i, 3) over(i) = True For j = 1 To m If i <> j And over(j) = False Then src = Trim(arr(i, 1)) & Trim(arr(i, 2)) & Trim(arr(i, 3)) des = Trim(arr(j, 1)) & Trim(arr(j, 2)) & Trim(arr(j, 3)) If src = des Then p = p + 1 s = s + 1 list(p, 1) = arr(j, 1) list(p, 2) = arr(j, 2) list(p, 3) = arr(j, 3) over(j) = True End If End If Next For n = p To p - s + 1 Step -1 list(n, 4) = s Next End If Next '-------------------------------------------- p = 0 For i = 1 To m s = 0 If over1(i) = False Then p = p + 1 s = s + 1 list1(p, 1) = list(i, 1) list1(p, 2) = list(i, 2) list1(p, 3) = list(i, 3) list1(p, 4) = list(i, 4) over1(i) = True For j = 1 To m If i <> j And over1(j) = False Then src = Trim(list(i, 2)) & Trim(list(i, 3)) des = Trim(list(j, 2)) & Trim(list(j, 3)) If src = des Then p = p + 1 s = s + 1 list1(p, 1) = list(j, 1) list1(p, 2) = list(j, 2) list1(p, 3) = list(j, 3) list1(p, 4) = list(j, 4) over1(j) = True End If End If Next For n = p To p - s + 1 Step -1 list1(n, 5) = s Next End If Next '-------------------------------------------- p = 0 For i = 1 To m If over2(i) = False Then p = p + 1 result(p, 1) = list1(i, 1) result(p, 2) = list1(i, 2) result(p, 3) = list1(i, 3) result(p, 4) = list1(i, 4) result(p, 5) = list1(i, 5) over2(i) = True For j = 1 To m If i <> j And over2(j) = False Then src = Trim(list1(i, 1)) & Trim(list1(i, 2)) & Trim(list1(i, 3)) des = Trim(list1(j, 1)) & Trim(list1(j, 2)) & Trim(list1(j, 3)) If src = des Then over2(j) = True Else Exit For End If End If Next End If Next '---------------------------------------------------- ReDim sort(1 To p, 1 To 5) As String ReDim sort1(1 To p, 1 To 9) As String ReDim over3(1 To p) As Boolean m = 0 pp = 0 For i = 1 To p s = 0 If over3(i) = False Then m = m + 1 s = s + 1 sort(m, 1) = result(i, 1) sort(m, 2) = result(i, 2) sort(m, 3) = result(i, 3) sort(m, 4) = result(i, 4) sort(m, 5) = result(i, 5) over3(i) = True For j = 1 To p If i <> j And over3(j) = False Then src = Trim(result(i, 2)) & Trim(result(i, 3)) des = Trim(result(j, 2)) & Trim(result(j, 3)) If src = des Then m = m + 1 s = s + 1 sort(m, 1) = result(j, 1) sort(m, 2) = result(j, 2) sort(m, 3) = result(j, 3) sort(m, 4) = result(j, 4) sort(m, 5) = result(j, 5) over3(j) = True End If End If Next For n = m - s + 1 To m - 1 For n1 = n + 1 To m If CInt(sort(n, 4)) < CInt(sort(n1, 4)) Then For tt = 1 To 5 temp = sort(n, tt) sort(n, tt) = sort(n1, tt) sort(n1, tt) = temp Next End If Next Next If s > 3 Then s1 = 3 Else s1 = s End If pp = pp + 1 For n = 1 To s1 If n = 1 Then sort1(pp, 1) = sort(m - s + n, 2) sort1(pp, 2) = sort(m - s + n, 3) sort1(pp, 3) = sort(m - s + n, 5) End If sort1(pp, 2 + 2 * n) = sort(m - s + n, 1) sort1(pp, 3 + 2 * n) = sort(m - s + n, 4) Next End If Next '------------------------------------------- '制表 '------------------------------------------- Range("d2").Resize(pp, 9) = sort1 End Sub
|