|
Option Explicit
Sub TEST7()
Dim ar, br, cr, dr(), er(), vResult(), isFlag As Boolean
Dim i&, j&, k&, r&, n&, iMax&, vKey, dic As Object, t#
Application.ScreenUpdating = False
Set dic = CreateObject("Scripting.Dictionary")
t = Timer
With [A1].CurrentRegion
ar = .Value
br = Intersect(.Offset(), .Offset(, 1))
.Interior.Color = xlNone
For i = 2 To UBound(ar)
dr = Application.Index(br, i)
ReDim er(1 To [L1].Value)
ReDim vResult(1 To WorksheetFunction.Combin(UBound(dr), [L1].Value))
combinArr dr, er, vResult, [L1].Value
For j = 1 To UBound(vResult)
dic(Join(vResult(j))) = dic(Join(vResult(j))) + 1
Next j
Next i
iMax = Application.Max(dic.Items)
ReDim br(1 To dic.Count, 1 To [L1].Value)
For Each vKey In dic.Keys
If dic(vKey) = iMax Then
cr = Split(vKey)
r = r + 1
For j = 0 To UBound(cr)
br(r, j + 1) = cr(j)
Next j
End If
Next
[L2].CurrentRegion.Offset(1).Clear
[L2].Resize(r, UBound(br, 2)) = br
[N1].Value = iMax
n = 2
For i = 1 To r
n = n + 1
If n = 56 Then n = 3
For k = 2 To UBound(ar)
dic.RemoveAll
For j = 2 To UBound(ar, 2)
dic(CStr(ar(k, j))) = j
Next j
isFlag = True
For j = 1 To UBound(br, 2)
If Not dic.Exists(br(i, j)) Then isFlag = False
Next j
If isFlag Then
For j = 1 To UBound(br, 2)
.Cells(k, dic(br(i, j))).Interior.ColorIndex = n
Next j
End If
Next k
Next i
End With
Set dic = Nothing
Application.ScreenUpdating = True
MsgBox "执行完毕!_用时: " & Format(Timer - t, "0.00") & " 秒", 64
End Sub
Function combinArr(ByRef ar(), ByRef br(), ByRef cr(), ByVal n&, Optional ByRef iGroup&, Optional ByVal iStart&, Optional ByVal iNum& = 1)
Dim i&, j&
For i = iStart + 1 To UBound(ar) - n + iNum
If iNum < n Then
br(iNum) = ar(i)
Call combinArr(ar, br, cr, n, iGroup, i, iNum + 1)
Else
br(iNum) = ar(i)
iGroup = iGroup + 1
cr(iGroup) = br
End If
Next
End Function
|
评分
-
1
查看全部评分
-
|