VBA & Macro Problems

Generate Digit Permutations in Excel with VBA

John WilsonJohn Wilson Nov 27, 2026 869 views

Question details

Generate all unique arrangements of the digits in a number and list them in an Excel column.

Generate Digit Permutations in Excel with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Use a recursive VBA macro to arrange the digits of a three- or four-digit input.
Observed behavior
The desired result is one unique digit arrangement per row, including arrangements with leading zeros. An earlier macro stopped with a runtime error at the row allocation line.
Before you start

Open the workbook in a desktop version of Excel that supports VBA, and select an unprotected worksheet. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm); the macro can create a new sheet or clear column A on the selected sheet.

Solution 1Recommended

Run a recursive VBA macro to list unique digit permutations

Use this macro to write each unique arrangement to column A while preserving leading zeros as text.

For distinct digits, three digits produce 6 arrangements and four produce 24. Repeated digits produce fewer unique arrangements. The macro limits input to seven digits to keep the output manageable.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. If the Developer tab is hidden, enable it through Excel's ribbon settings.

2
Insert the macro

In the VBA editor, select Insert > Module and paste the following code into the new standard module: Option Explicit Private CurrentRow As Long Private OutputSheet As Worksheet Public Sub GenerateDigitPermutations() Dim digits As String Dim choice As VbMsgBoxResult digits = InputBox("Enter 2 to 7 digits to permute:") If digits = "" Then Exit Sub If Len(digits) < 2 Or Len(digits) > 7 Or digits Like "*[!0-9]*" Then MsgBox "Enter 2 to 7 digits only." Exit Sub End If choice = MsgBox("Create a new sheet?" & vbCrLf & _ "Choose No to clear column A on the active sheet.", _ vbYesNoCancel + vbQuestion) If choice = vbCancel Then Exit Sub If choice = vbYes Then Set OutputSheet = Worksheets.Add Else Set OutputSheet = ActiveSheet End If OutputSheet.Columns("A").Clear OutputSheet.Columns("A").NumberFormat = "@" CurrentRow = 1 WritePermutations "", digits MsgBox "Created " & (CurrentRow - 1) & " unique permutations." End Sub Private Sub WritePermutations(ByVal prefix As String, ByVal remaining As String) Dim i As Long Dim digit As String Dim seen As String If Len(remaining) = 0 Then OutputSheet.Cells(CurrentRow, 1).Value2 = prefix CurrentRow = CurrentRow + 1 Exit Sub End If For i = 1 To Len(remaining) digit = Mid$(remaining, i, 1) If InStr(1, seen, digit, vbBinaryCompare) = 0 Then seen = seen & digit WritePermutations prefix & digit, _ Left$(remaining, i - 1) & Mid$(remaining, i + 1) End If Next i End Sub

3
Run the macro

Return to Excel, select Developer > Macros, choose GenerateDigitPermutations, and select Run. Enter the digits, then choose Yes to create a sheet or No to clear and reuse column A.

4
Check the results

Look in column A of the output sheet. For example, enter 123 to get six rows, or 1234 to get 24 rows. The results are stored as text so a leading zero remains visible.

Run a recursive VBA macro to list unique digit permutations
If a row error occurs: Make sure the complete code is in one standard module and that CurrentRow is declared as Long and initialized to 1. Also check that the output sheet is not protected and that column A can be cleared.
Free Microsoft Office alternative

Work with spreadsheet lists in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work. The Excel VBA macro above is specific to Excel; check macro support in your WPS version before relying on it.

Organize and review digit permutation lists in WPS SpreadsheetsOpen and work with common Microsoft Excel formats, including .xlsxUse a familiar spreadsheet interface for a smoother transition from ExcelGet a free, lightweight suite for everyday document and spreadsheet work
microsoft office alternative - wps office

Frequently Asked Questions

How many permutations will four digits produce?

Four distinct digits produce 24 arrangements. If any digits repeat, the macro skips duplicate arrangements, so the result will contain fewer rows.

Will a permutation that starts with zero keep the zero?

Yes. The macro sets column A to text format before writing the results, so an arrangement such as 012 remains visible as 012.

Why does the macro stop on a row assignment?

Check that CurrentRow is declared as Long at the top of the module and set to 1 before the recursive procedure starts. Also select an unprotected worksheet and make sure column A can be cleared.

Can I keep the existing values in column A?

Yes. When the macro asks whether to create a new sheet, select Yes. Selecting No clears column A on the active worksheet.

How do I save the macro for later use?

In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). When reopening the file, enable macros only if you trust the workbook.