这个排列组合经常要用到,托您的问题,我学习了一下,做了一个排列组合的代码。只当抛砖引玉,希望有高手批评指正。(这个程序是将10个数字,按每条4个数字进行组合)
Sub 组合()
Dim arr_S(1 To 10)
Dim arr_O
Dim I, J, K1, K2, K3, K4 As Integer
I = Sheet1.Range("A65536").End(xlUp).Row
'arr_S = Sheets("sheet1").Range("a2:a" & I) 这个运行时出错
For J = 1 To I - 1 '给原始序列数组赋值
arr_S(J) = Cells(J + 1, 1)
Next J
I = I - 1 '原始数据的个数
J = I * (I - 1) * (I - 2) * (I - 3) / (1 * 2 * 3 * 4) '按4列组合,计算目标数组的个数
ReDim arr_O(1 To J, 1 To 4)
J = 1
For K1 = 1 To I - 3
For K2 = K1 + 1 To I - 2
For K3 = K2 + 1 To I - 1
For K4 = K3 + 1 To I
arr_O(J, 1) = arr_S(K1)
arr_O(J, 2) = arr_S(K2)
arr_O(J, 3) = arr_S(K3)
arr_O(J, 4) = arr_S(K4)
J = J + 1
Next K4
Next K3
Next K2
Next K1
For I = 1 To J - 1
Cells(I + 1, 3) = arr_O(I, 1)
Cells(I + 1, 4) = arr_O(I, 2)
Cells(I + 1, 5) = arr_O(I, 3)
Cells(I + 1, 6) = arr_O(I, 4)
Next I
End Sub
执行结果如图所示: