|
Option Explicit
Sub test()
Dim ar()
ar = combinArr2(20, 10)
[A1].CurrentRegion.Clear
[A1].Resize(UBound(ar), UBound(ar, 2)) = ar
End Sub
Function combinArr2(ByVal m&, ByVal n&)
Dim br&(), vResult, i&, j&, iCount&, iGroup&, vTemp
iGroup = Application.Combin(m, n)
ReDim vResult(1 To iGroup, 1 To n)
ReDim br&(1 To n)
If n = 1 Then
For i = 1 To iGroup
vResult(i, 1) = i
Next i
combinArr2 = vResult
Exit Function
End If
For j = 1 To n - 1: br(j) = j: Next
iCount = 0
Do
For i = br(n - 1) + 1 To m
br(n) = i
iCount = iCount + 1
For j = 1 To n
vResult(iCount, j) = br(j)
Next j
Next
If br(n - 1) < br(n) - 1 Then
br(n - 1) = br(n - 1) + 1
Else
For j = n - 2 To 1 Step -1
If br(j) <> br(j + 1) - 1 Then
vTemp = br(j) + 1: br(j) = vTemp: j = j + 1
Do Until j = n
br(j) = br(j - 1) + 1: j = j + 1
Loop
Exit For
End If
Next
End If
Loop Until iCount = iGroup
combinArr2 = vResult
End Function
|
|