|
Option Explicit
Sub test()
Dim i&, dr(), er(), vResult(), t#, r&
t = Timer
dr = Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value)
ReDim er(1 To [B1].Value)
ReDim vResult(1 To WorksheetFunction.Combin(UBound(dr), [B1].Value), 1 To [B1].Value)
combinArr dr, er, vResult, [B1].Value, r
[E1].CurrentRegion.Clear
With [E1].Resize(UBound(vResult), [B1].Value)
.Value = vResult
.EntireColumn.AutoFit
End With
MsgBox "执行完毕!_用时: " & Format(Timer - t, "0.00") & " 秒,共发现 " & UBound(vResult) & " 组组合", 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
For j = 1 To n
cr(iGroup, j) = br(j)
Next
End If
Next
End Function
|
|