Hi PGC,
The below code doesnt work well Excel 2003 where the max. row count is 65536. When I change Const lMaxRows As Long = 1000000 to =65536, it fills further into col. L through Q, generating 131072 combinations; but it doesnt go to col. S to fill the balance options.
What modification is possible so that it automatically moves to next set of columns to fill the entire results.
Thanks.
Code:
Sub WriteArray(vResultAll As Variant, rOut As Range)
Dim vArrTemp As Variant, lMaxArrTemp As Long
Dim p As Long, lMaxChunks As Long
Dim j As Long, k As Long, i As Long
Const lMaxRows As Long = 1000000
Application.ScreenUpdating = False
p = UBound(vResultAll, 2)
lMaxChunks = (UBound(vResultAll) - 1) \ lMaxRows
rOut.Resize(lMaxRows, (lMaxChunks + 1) * (p + 2)).Clear
' write chunks of lMaxRows
For j = 0 To lMaxChunks
lMaxArrTemp = IIf((j + 1) * lMaxRows + 1 < UBound(vResultAll), lMaxRows, UBound(vResultAll) Mod lMaxRows)
ReDim vArrTemp(1 To lMaxArrTemp, 1 To p)
For k = 1 To lMaxArrTemp
For i = 1 To p
vArrTemp(k, i) = vResultAll(j * lMaxRows + k, i)
Next i
Next k
rOut.Offset(0, j * (p + 2)).Resize(lMaxArrTemp, p).Value = vArrTemp
Next j
Application.ScreenUpdating = True
End Sub
In this test I generated combinations with repetition of 30 elements taken 6 at a time. The total number of combinations is 1,623,160. The code wrote the first million in columns D:I and the rest in L:Q
Inputs:
B1 - 6
B2 - True
B3 - True
B5:B34 - numbers 1-30
[TABLE="width: 2"]
<tbody>[TR]
[TH] [/TH]
[TH="align: center"]A[/TH]
[TH="width: 30, align: center"]B[/TH]
[TH="width: 30, align: center"]C[/TH]
[TH="width: 30, align: center"]D[/TH]
[TH="width: 30, align: center"]E[/TH]
[TH="width: 30, align: center"]F[/TH]
[TH="width: 30, align: center"]G[/TH]
[TH="width: 30, align: center"]H[/TH]
[TH="width: 30, align: center"]I[/TH]
[TH="width: 30, align: center"]J[/TH]
[TH="width: 30, align: center"]K[/TH]
[TH="width: 30, align: center"]L[/TH]
[TH="width: 30, align: center"]M[/TH]
[TH="width: 30, align: center"]N[/TH]
[TH="width: 30, align: center"]O[/TH]
[TH="width: 30, align: center"]P[/TH]
[TH="width: 30, align: center"]Q[/TH]
[TH="width: 30, align: center"]R[/TH]
[/TR]
[TR]
[TD="align: center"]
1[/TD]
[TD="align: left"]p[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
2[/TD]
[TD="align: left"]Comb[/TD]
[TD="align: center"]TRUE[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
3[/TD]
[TD="align: left"]Repet[/TD]
[TD="align: center"]TRUE[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
4[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
5[/TD]
[TD="align: left"]Set[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
6[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]2[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
7[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]3[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
8[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]4[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
9[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
10[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]6[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
11[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]7[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
12[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]8[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
13[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]9[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]13[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
14[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]10[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]14[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
15[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]11[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]15[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
16[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]12[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]26[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
17[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]13[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]27[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
18[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]14[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]28[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
19[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]15[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]29[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
20[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]16[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]17[/TD]
[TD="align: right"]30[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
21[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]17[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
22[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]18[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]19[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
23[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]19[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]23[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]20[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
24[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]20[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]24[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]21[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="align: center"]
25[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]21[/TD]
[TD="align: right"] [/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"] [/TD]
[TD="align: right"] [/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]16[/TD]
[TD="align: right"]18[/TD]
[TD="align: right"]22[/TD]
[TD="align: right"] [/TD]
[/TR]
[TR]
[TD="colspan: 19"][Book1.xlsm]Sheet1[/TD]
[/TR]
</tbody>[/TABLE]
Yes, it can get very slow if you write cell by cell.
It's much quicker if you either copy a whole range, like
Code:
Range("J1:J10000").Value = Range("A1:A10000").Value
or if you write an array in one statement, ex:
Code:
Dim lArr(1 To 10000, 1 To 3)
' some code that puts values in the array
Range("A1:C10000").Value = lArrr
Remark: Sorry, I did not understand the rest of your post.
Hope this helps.[/QUOTE]