- Sub zz()
- Dim ar, br, b(), n, m, k, t As Boolean
- Dim d As Object, dd As Object, [color=Red]accum&, i&, j&[/color]
- Set d = CreateObject("scripting.dictionary")
- Set dd = CreateObject("scripting.dictionary")
- br = Range("a2:c" & [a65536].End(3).Row).Value
- ar = br
- ReDim b(UBound(ar))
- For i = 1 To UBound(ar) - 1
- m = ar(i, 1)
- For j = i + 1 To UBound(ar)
- If m > ar(j, 1) Then m = ar(j, 1): k = j: t = True
- Next
- If t Then
- t = False
- For j = 1 To UBound(ar, 2)
- b(j) = ar(i, j): ar(i, j) = ar(k, j): ar(k, j) = b(j)
- Next
- End If
- Next
- [color=Red]accum = 1[/color]
- d(ar(1, 1)) = 1: ar(1, 3) = 1
- dd(ar(1, 1) & ar(1, 2)) = ar(1, 3)
- For i = 2 To UBound(ar)
- If d.exists(ar(i, 1)) Or d.exists(ar(i, 2)) Then
- ar(i, 3) = Application.Max(d(ar(i, 1)), d(ar(i, 2)))
- Else
- [color=Red]ar(i, 3) = accum + 1: accum = ar(i, 3)[/color]
- End If
- For j = 1 To UBound(ar, 2) - 1
- d(ar(i, j)) = ar(i, 3)
- Next
- dd(ar(i, 1) & ar(i, 2)) = ar(i, 3)
- Next
- For i = 1 To UBound(br)
- br(i, 3) = dd(br(i, 1) & br(i, 2))
- Next
- [a2].Resize(UBound(ar), 3) = br
- Set d = Nothing: Set dd = Nothing
- End Sub
复制代码 |