You are here:

# Excel/Generating combinations of six numbers out of 45 numbers

Question
I fund this macro from a friend, and the problem is that when I run it on excel 2007 and excel 2010 it gives an error #104. The sheet can only show up to row number 1047531. how can I make it to show all the combinations on one or more sheets?

"
Option Explicit
Option Base 1

Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer, F As Integer
Dim nNb() As Integer, I As Integer, J As Double

Sub GetCombin()
' Program to generate 6-number combinations
Application.ScreenUpdating = False
ReDim nNb(20)
Range("A1").Select
I = 1
' Allow up to 20 numbers starting in A1 and descending
Do Until I = 21
If ActiveCell.Offset(I - 1, 0) = "" Then Exit Do
nNb(I) = ActiveCell.Offset(I - 1, 0).Value
I = I + 1
Loop
ReDim Preserve nNb(I)
I = I - 1
J = 1
' Place combinations in columns C to H starting at row 2
For A = 1 To I - 5
For B = A + 1 To I - 4
For C = B + 1 To I - 3
For D = C + 1 To I - 2
For E = D + 1 To I - 1
For F = E + 1 To I
ActiveCell.Offset(J, 2).Value = nNb(A)
ActiveCell.Offset(J, 3).Value = nNb(B)
ActiveCell.Offset(J, 4).Value = nNb(C)
ActiveCell.Offset(J, 5).Value = nNb(D)
ActiveCell.Offset(J, 6).Value = nNb(E)
ActiveCell.Offset(J, 7).Value = nNb(F)
J = J + 1
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
End Sub
"

Instructions:
- Starting in A1 and down, place up to 20 numbers you want to use in 6-number combinations.
- Start the macro and bingo.
- Starting in C2 to H2 and down, you will get your all your combinations fairly quickly depending on your computer and how many numbers you choose.
- Make cosmetic changes as you want (headings, column widths, etc.).
- Filter, erase, choose or do what you want with those combinations

Daniel,

Option Base 1

Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer, F As Integer
Dim nNb() As Integer, I As Integer, J As Double

Sub GetCombin()
' Program to generate 6-number combinations
Application.ScreenUpdating = False
ReDim nNb(20)
Range("A1").Select
I = 1
' Allow up to 20 numbers starting in A1 and descending
Do Until I = 21
If ActiveCell.Offset(I - 1, 0) = "" Then Exit Do
nNb(I) = ActiveCell.Offset(I - 1, 0).Value
I = I + 1
Loop
ReDim Preserve nNb(I)
I = I - 1
J = 1
' Place combinations in columns C to H starting at row 2
For A = 1 To I - 5
For B = A + 1 To I - 4
For C = B + 1 To I - 3
For D = C + 1 To I - 2
For E = D + 1 To I - 1
For F = E + 1 To I
ActiveCell.Offset(J, 2).Value = nNb(A)
ActiveCell.Offset(J, 3).Value = nNb(B)
ActiveCell.Offset(J, 4).Value = nNb(C)
ActiveCell.Offset(J, 5).Value = nNb(D)
ActiveCell.Offset(J, 6).Value = nNb(E)
ActiveCell.Offset(J, 7).Value = nNb(F)
J = J + 1
' check if we are beyond the end of the sheet, if so add a new sheet
' to the end of the tab order and begin writing there.
if j > rows.count then
J = 1
End if
Next F
Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
End Sub

In excel 2007 and later
If you are doing up to 15 numbers, then they should all be on one sheet.

When you get up to 20, the results should take most of 11 sheets.

--
Regards,
Tom Ogilvy

Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 9 Politeness = 9 Comment Thank you very much for taking your time to answer my question.

This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

#### Tom Ogilvy

##### Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

##### Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.