Generate Every Five-Number Combination in Excel with VBA
Question details
Generate every unique five-number combination from a list of up to 30 values in Excel.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user has a list of lottery numbers and wants each possible five-number set written to a worksheet.
- Observed behavior
- Each combination should appear once, with its five values in separate cells.
Enter 5 to 30 distinct numeric values in consecutive cells in column A, beginning at A1, on the sheet you will use as input. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and make sure you can run trusted macros.
Generate combinations with a five-loop Excel VBA macro
Use this macro to write every unique five-number combination to a new worksheet without overwriting the input list.
The values in each combination follow their order in column A, not necessarily numerical order. With 30 distinct values, the macro produces 142,506 combinations.
The macro temporarily turns off screen updating and automatic calculation, then restores their previous settings even if an error occurs.
Select the worksheet containing the values in column A. Open the VBA editor from Developer > Visual Basic, or press Alt+F11 in Excel for Windows.
In the VBA editor, choose Insert > Module and paste the following code: Sub GenerateFiveNumberCombinations() Dim src As Worksheet, output As Worksheet Dim lastRow As Long, n As Long, total As Long, r As Long Dim i As Long, j As Long, k As Long, l As Long, m As Long, x As Long Dim values() As Double, results() As Variant Dim oldScreen As Boolean, oldCalc As XlCalculation Dim errorText As String Set src = ActiveSheet lastRow = src.Cells(src.Rows.Count, 1).End(xlUp).Row If lastRow < 5 Or lastRow > 30 Then MsgBox "Enter 5 to 30 distinct numbers in A1 and below." Exit Sub End If n = lastRow ReDim values(1 To n) For i = 1 To n If IsError(src.Cells(i, 1).Value) Then MsgBox "Column A must contain numbers without errors or gaps." Exit Sub End If If Not IsNumeric(src.Cells(i, 1).Value) Or IsEmpty(src.Cells(i, 1).Value) Then MsgBox "Column A must contain numbers without errors or gaps." Exit Sub End If values(i) = CDbl(src.Cells(i, 1).Value) For x = 1 To i - 1 If values(i) = values(x) Then MsgBox "Each number in column A must be distinct." Exit Sub End If Next x Next i total = CLng(Application.WorksheetFunction.Combin(n, 5)) ReDim results(1 To total, 1 To 5) oldScreen = Application.ScreenUpdating oldCalc = Application.Calculation On Error GoTo HandleError Application.ScreenUpdating = False Application.Calculation = xlCalculationManual For i = 1 To n - 4 For j = i + 1 To n - 3 For k = j + 1 To n - 2 For l = k + 1 To n - 1 For m = l + 1 To n r = r + 1 results(r, 1) = values(i) results(r, 2) = values(j) results(r, 3) = values(k) results(r, 4) = values(l) results(r, 5) = values(m) Next m Next l Next k Next j Next i Set output = src.Parent.Worksheets.Add(After:=src.Parent.Worksheets(src.Parent.Worksheets.Count)) output.Range("A1").Resize(total, 5).Value = results CleanUp: Application.Calculation = oldCalc Application.ScreenUpdating = oldScreen If Len(errorText) > 0 Then MsgBox "The macro stopped: " & errorText Else MsgBox total & " combinations written to a new worksheet." End If Exit Sub HandleError: errorText = Err.Description Resume CleanUp End Sub
Return to the input worksheet, then open Developer > Macros, select GenerateFiveNumberCombinations, and choose Run. The results appear in columns A through E of a new worksheet.

Review lottery combination worksheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files to review and work with combination lists as you migrate between office suites; the Excel VBA macro itself is not guaranteed to run in WPS.

Frequently Asked Questions
How many five-number combinations are there from 30 values?
There are 142,506 unique combinations. In Excel, enter =COMBIN(30,5) in a cell to calculate the count.
Why must the numbers in column A be distinct?
The macro uses list positions to build combinations. Duplicate values could make different selections look identical, so it checks column A and stops if it finds a duplicate.
Does the macro sort each combination numerically?
No. Each combination follows the order of the input values in column A. Sort the input list before running the macro if you want the values within each row in ascending order.
How can I keep only combinations containing a specific number?
After generating the rows, add a helper column with a formula such as =COUNTIF(A1:E1,23)>0, fill it down, and use Data > Filter to show TRUE rows. Replace 23 with the required number.
Why does Excel ask me to enable macros?
Excel restricts VBA execution for security. Open a workbook you trust, then use the security warning's Enable Content button if prompted. Save the macro in an .xlsm file so it is retained.




