a1~a39, 填入1~39,然後耐心等= =
Sub test()
Dim n As Variant, c As Double
n = Application.Transpose(Range("A1", Range("A1").End(xlDown)))
Application.ScreenUpdating = False
For i = 1 To UBound(n)
For j = i + 1 To UBound(n)
For k = j + 1 To UBound(n)
For l = k + 1 To UBound(n)
For m = l + 1 To UBound(n)
c = c + 1
Cells(c, 2) = n(i)
Cells(c, 3) = n(j)
Cells(c, 4) = n(k)
Cells(c, 5) = n(l)
Cells(c, 6) = n(m)
Next m
Next l
Next k
Next j
Next i
Application.ScreenUpdating = True
MsgBox c & "組"
End Sub作者: n7822123 時間: 2020-2-22 00:21
謝謝教學,用你教的方法
改寫前跑3分鐘,改寫後6秒左右跑完 ^^
Sub test2()
Dim m As Integer, n As Integer, c As Double, t(1 To 39) As Integer, r As Double, arr()
For i = 1 To 39: t(i) = i: Next i
m = 39
n = 5
t0 = Timer
c = Cmn(m) / (Cmn(m - n) * Cmn(n))
ReDim arr(1 To c, 1 To n)
Application.ScreenUpdating = False
For i = 1 To m
For j = i + 1 To m
For k = j + 1 To m
For l = k + 1 To m
For p = l + 1 To m
r = r + 1
arr(r, 1) = t(i)
arr(r, 2) = t(j)
arr(r, 3) = t(k)
arr(r, 4) = t(l)
arr(r, 5) = t(p)
Next p
Next l
Next k
Next j
Next i
Range(Cells(1, 1), Cells(r, 5)) = arr()
Application.ScreenUpdating = True
MsgBox r & "組:使用時間" & Timer - t0 & "秒"
End Sub
Function Cmn(x As Integer) As Double
Dim i As Integer, y As Double
y = 1
For i = 1 To x
y = y * i
Next
Cmn = y
End Function