VBA & Macro Problems

Generate Six-Number Combinations in Excel with VBA

Jason MillerJason Miller Nov 27, 2026 869 views

Question details

Generate every unique six-number combination from a specified range and distribute the results across Excel worksheets.

Generate Six-Number Combinations in Excel with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
The user wants to generate combinations without repeated numbers, including combinations from 1 through 75.
Observed behavior
The desired output is one ascending combination per row, with a new worksheet added whenever the current sheet fills.
Before you start

Use a desktop version of Excel that supports VBA, and make sure macros are enabled for a trusted workbook. Test the procedure with a small range first: 1 through 75 produces 201,359,550 combinations, which is likely to be impractical to hold in one workbook.

Solution 1Recommended

Generate combinations in batches with an Excel VBA macro

Use this macro to write unique combinations to a new workbook, adding worksheets as they fill.

The macro writes combinations in batches and keeps each row in ascending order, so permutations such as 1, 2 and 2, 1 are not counted separately. Set n to the largest number in the range and k to the combination size.

Even with batched writes, the full 1–75 result requires 193 worksheets in a modern Excel workbook and may exceed available memory or take a very long time. For the full dataset, consider generating separate files with a tool designed for large data exports.

1
Open the VBA editor

In desktop Excel, open a trusted workbook, press Alt+F11, and select Insert > Module in the VBA editor.

2
Paste the macro

Paste the following code into the module: Sub GenerateCombinations() Dim n As Long, k As Long, i As Long, j As Long Dim filled As Long, outputRow As Long Dim a() As Long, buffer() As Variant Dim wb As Workbook, ws As Worksheet On Error GoTo Failed n = 75 k = 6 If k < 1 Or k > n Then Err.Raise vbObjectError + 1, , "Choose k between 1 and n." ReDim a(1 To k) ReDim buffer(1 To 10000, 1 To k) For i = 1 To k a(i) = i Next i Application.ScreenUpdating = False Set wb = Workbooks.Add(xlWBATWorksheet) Set ws = wb.Worksheets(1) outputRow = 1 Do If outputRow > ws.Rows.Count Then Set ws = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count)) outputRow = 1 End If filled = filled + 1 For j = 1 To k buffer(filled, j) = a(j) Next j If filled = 10000 Or outputRow + filled - 1 = ws.Rows.Count Then ws.Cells(outputRow, 1).Resize(filled, k).Value = buffer outputRow = outputRow + filled filled = 0 End If i = k Do While i > 0 If a(i) < n - k + i Then Exit Do i = i - 1 Loop If i = 0 Then Exit Do a(i) = a(i) + 1 For j = i + 1 To k a(j) = a(j - 1) + 1 Next j Loop If filled > 0 Then ws.Cells(outputRow, 1).Resize(filled, k).Value = buffer Application.ScreenUpdating = True MsgBox "Combination generation is complete." Exit Sub Failed: Application.ScreenUpdating = True MsgBox "Generation stopped: " & Err.Description End Sub

3
Test a smaller range

In the code, change n = 75 to n = 10 and leave k = 6. Click inside GenerateCombinations and press F5; the new workbook should contain 210 rows.

4
Set the desired range and run

Change n to 26, 35, 54, 69, or 75 as needed, keep k = 6, and press F5. Use File > Save As to save the result workbook if Excel can accommodate it.

Generate combinations in batches with an Excel VBA macro
Keep the macro separately: Save the workbook containing the VBA code as an Excel Macro-Enabled Workbook (.xlsm) if you want to reuse the macro. The generated results are placed in a separate workbook.
Free Microsoft Office alternative

Review combination spreadsheets with WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel file formats, making it easy to move and review smaller result workbooks. It does not fix Excel VBA issues, and macro support can vary by version and platform.

Open and review combination tables in WPS SpreadsheetsWork with Microsoft Excel formats such as .xlsx and .xlsUse a familiar spreadsheet interface for an easier transitionChoose a free, lightweight alternative for everyday office tasks
microsoft office alternative - wps office

Frequently Asked Questions

How many six-number combinations are there from 1 to 75?

There are 201,359,550 unique combinations, calculated as 75 choose 6. With 1,048,576 rows per modern Excel worksheet and no header rows, they require 193 worksheets.

How do I generate combinations from 1 to 35 instead?

In the VBA module, change n = 75 to n = 35, leave k = 6, and run GenerateCombinations with F5.

Why does the number 1 appear in many rows?

That is expected: many valid combinations include 1. The macro keeps numbers in ascending order and does not repeat a number within the same row.

Can I run this macro in Excel for the web?

No. Excel for the web cannot run VBA macros. Open the workbook in a desktop version of Excel that supports VBA, then use the VBA editor to run it.

What if Excel cannot handle the full output?

Test a smaller range first by changing n in the VBA module. For all 201,359,550 rows, use a tool designed to generate and store large datasets in separate files rather than relying on a single Excel workbook.