Generate Unique Permutations and Combinations in Excel VBA
Question details
Generate unique groups of a chosen size from values in column A using Excel VBA.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants to list combinations or permutations without repeating a value within each group.
- Observed behavior
- Each combination should appear once regardless of order; permutations should include distinct orderings of the same values.
Place the source values in column A of the active worksheet, with no header. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm), and enable macros only if you trust the workbook and its code.
Run a VBA macro to generate unique groups
Use this macro to choose a group size and generate either combinations or permutations on a new worksheet.
The macro ignores blank and error cells, trims surrounding spaces, and removes duplicate text values using case-sensitive comparison. Combinations ignore order, while permutations treat different orders as distinct.
In Excel, open the worksheet containing your values in column A. Open Developer > Visual Basic, or press Alt+F11 where available.
In the VBA editor, select Insert > Module and paste this code into the module: Option Explicit Private items() As String Private chosen() As Long Private used() As Boolean Private itemCount As Long, groupSize As Long, outputRow As Long Private makePermutations As Boolean, stopped As Boolean Private outputSheet As Worksheet Public Sub GenerateUniqueGroups() Dim sourceSheet As Worksheet, c As Range Dim lastRow As Long, i As Long, v As Variant Dim textValue As String, duplicate As Boolean Dim sizeInput As Variant, choice As VbMsgBoxResult Set sourceSheet = ActiveSheet lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, 1).End(xlUp).Row ReDim items(1 To lastRow) itemCount = 0 For Each c In sourceSheet.Range("A1:A" & lastRow) v = c.Value2 If Not IsError(v) Then textValue = Trim$(CStr(v)) If Len(textValue) > 0 Then duplicate = False For i = 1 To itemCount If StrComp(items(i), textValue, vbBinaryCompare) = 0 Then duplicate = True Exit For End If Next i If Not duplicate Then itemCount = itemCount + 1 items(itemCount) = textValue End If End If End If Next c If itemCount < 2 Then MsgBox "Enter at least two distinct values in column A." Exit Sub End If sizeInput = Application.InputBox("Enter the group size (at least 2):", "Group size", Type:=1) If VarType(sizeInput) = vbBoolean Then Exit Sub If sizeInput < 2 Or sizeInput > itemCount Or sizeInput <> Fix(sizeInput) Then MsgBox "Enter a whole number from 2 to " & itemCount & "." Exit Sub End If If sizeInput > sourceSheet.Columns.Count Then MsgBox "The group size exceeds the worksheet column limit." Exit Sub End If groupSize = CLng(sizeInput) choice = MsgBox("Generate permutations? Select No for combinations.", vbYesNoCancel + vbQuestion) If choice = vbCancel Then Exit Sub makePermutations = (choice = vbYes) ReDim chosen(1 To groupSize) ReDim used(1 To itemCount) Set outputSheet = sourceSheet.Parent.Worksheets.Add(After:=sourceSheet) outputSheet.Range(outputSheet.Cells(1, 1), outputSheet.Cells(1, groupSize)).EntireColumn.NumberFormat = "@" outputRow = 1 stopped = False BuildGroups 1, 1 If stopped Then MsgBox "The worksheet row limit was reached; the output is incomplete." Else MsgBox "Generated " & (outputRow - 1) & " groups on the new worksheet." End If End Sub Private Sub BuildGroups(ByVal depth As Long, ByVal firstIndex As Long) Dim i As Long, j As Long, nextIndex As Long If stopped Then Exit Sub If depth > groupSize Then If outputRow > outputSheet.Rows.Count Then stopped = True Exit Sub End If For j = 1 To groupSize outputSheet.Cells(outputRow, j).Value = items(chosen(j)) Next j outputRow = outputRow + 1 Exit Sub End If For i = firstIndex To itemCount If Not used(i) Then chosen(depth) = i used(i) = True If makePermutations Then nextIndex = 1 Else nextIndex = i + 1 End If BuildGroups depth + 1, nextIndex used(i) = False If stopped Then Exit Sub End If Next i End Sub
Return to the source worksheet, select Developer > Macros, choose GenerateUniqueGroups, and click Run. Enter a whole-number group size of at least 2, then choose Yes for permutations or No for combinations.
Open the new worksheet added after the source sheet. Each row is one group, with its values in separate columns.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and move your spreadsheet work into WPS with minimal disruption. VBA macro support varies by WPS version and configuration, so check compatibility before relying on this Excel macro.

Frequently Asked Questions
What is the difference between a combination and a permutation?
A combination ignores order, so A–B and B–A are the same group. A permutation treats them as two distinct groups. In the macro prompt, select Yes for permutations or No for combinations.
What happens if column A contains duplicate values?
The macro keeps one instance of each value after trimming surrounding spaces. Its comparison is case-sensitive, so A and a are treated as different values.
Why can't I run the macro after reopening the workbook?
Save the workbook using File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). When reopening it, enable macros if Excel prompts you and you trust the workbook.
Can I use a header in column A?
The macro reads from A1 through the last used cell in column A, so it treats a header as a source value. Remove the header or change the range in the code to start at A2.




