You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- Generating combinations of six numbers out of 49 numbers

Advertisement

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

ANSWER: This program, as written, will run fine on all versions of Office as there are 38760 combinations of 20 numbers in a pick 6 from 49 situation - IF you want ALL possible combinations of 49 numbers then a modification of the macro would do it. Just one warning - there are almost 14 million possible combinations of a 6 from 49!

Sub GetCombin()

' Program to generate 6-number combinations

Application.ScreenUpdating = False

ReDim nNb(49)

Range("A1").Select

I = 1

' Allow up to 20 numbers starting in A1 and descending

Do Until I = 49

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

Cells(J, 2).Value = nNb(A)

Cells(J, 3).Value = nNb(B)

Cells(J, 4).Value = nNb(C)

Cells(J, 5).Value = nNb(D)

Cells(J, 6).Value = nNb(E)

Cells(J, 7).Value = nNb(F)

J = J + 1

If J > 65536 Then 'Amend this number to however many rows you want - 65536 is the excel

'2003 and earlier limit

J = 1

Sheets.Add

End If

Next F

Next E

Next D

Next C

Next B

Next A

Application.ScreenUpdating = True

End Sub

---------- FOLLOW-UP ----------

QUESTION: 1. How can I make it to show only combinations with a sum of 128?

2. Or calculate some descriptive statistics on them?

To return only those items that add to 128 amend this section

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

to

For F = E + 1 To I

if nNb(A)+nNb(B)+nNb(C)+nNb(D)+nNb(E)+nNb(F)=128 THEN

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

END IF

Next F

As to calculate descriptive stats, what did you have in mind?!

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | thank you once more, you have really been helpful to me. |

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

Excel

Answers by Expert:

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.**Education/Credentials**

I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!