Generate Digit Permutations in Excel with VBA
Question details
Generate all unique arrangements of the digits in a number and list them in an Excel column.

- 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.
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.
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.
In Excel, open the Developer tab and select Visual Basic. If the Developer tab is hidden, enable it through Excel's ribbon settings.
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
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.
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.

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.

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.




