Need vba code to generate all combinations from 1 to 10 character length for 1,2,a,b,c,A,B,C,D

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
In other words, generate all of the single characters, double characters, all the way up to 10 characters which would be 1111111111, or, aaaaaaaaaa, or DDDDDDDDDD, or everything in between.

The results can be put in the A column or more if needed.

No Delimiters in between the results.

Example:
1
2
a
b
c
A
B
C
D
11
12
1a
1b
etc ...
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Here's some code written by @pgc01 for another thread in this forum.

To illustrate, I've shown the 91 combinations generated for 13 elements in combinations of 2 with repeats
=COMBIN(13+2-1,2)

ABCDE
1Choose211
2Combins?TRUE12
3Repeats?TRUE1a
41b
511c
621A
7a1B
8b1C
9c1D
10A111
11B112
12C11a
13D11b
141122
15122a
161a2b
171b2c
182A
192B
202C
212D
22211
23212
2421a
2521b
26aa
27ab
28ac
29aA
30aB
31aC
32aD
33a11
34a12
35a1a
36a1b
37bb
38bc
39bA
40bB
41bC
42bD
43b11
44b12
45b1a
46b1b
47cc
48cA
49cB
50cC
51cD
52c11
53c12
54c1a
55c1b
56AA
57AB
58AC
59AD
60A11
61A12
62A1a
63A1b
64BB
65BC
66BD
67B11
68B12
69B1a
70B1b
71CC
72CD
73C11
74C12
75C1a
76C1b
77DD
78D11
79D12
80D1a
81D1b
821111
831112
84111a
85111b
861212
87121a
88121b
891a1a
901a1b
911b1b
Sheet2

VBA Code:
' PGC Set 2007
' Calculates and writes the Combinations / Permutations with/without repetition
' vElements - Array with the set elements (1 to n)
' p - number of elements in 1 combination/permutation
' bComb - True: Combinations, False: Permutations
' bRepet - True: with repetition, False: without repetition
' vResult - Array to hold 1 permutation/combination (1 to p)
' lRow - row number. the next combination/permutation is written in lRow+1
' vResultAll - Array to hold all the permutations/combinations (1 to Total, 1 to p)
' iElement - order of the element to process in case of combination
' iIndex - position of the next element in the combination/permutation
' Sub CombPerm() deals with the input / output
' Sub CombPermNP() generates the combinations / permutations
Sub CombPerm()
Dim rRng As Range, p As Integer
Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
Dim lRow As Long, bComb As Boolean, bRepet As Boolean
 
' Get the inputs and clear the result range (you may adjust for other locations)
Set rRng = Range("B5", Range("B5").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
bComb = Range("B2")
bRepet = Range("B3")
 
Columns("D").Resize(, p + 1).Clear
 
' Error
If (Not bRepet) And (rRng.Count < p) Then
    MsgBox "With no repetition the number of elements of the set must be bigger or equal to p"
    Exit Sub
End If
 
' Set up the arrays for the set elements and the result
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
With Application.WorksheetFunction
    If bComb = True Then
            lTotal = .Combin(rRng.Count + IIf(bRepet, p - 1, 0), p)
    Else
        If bRepet = False Then lTotal = .Permut(rRng.Count, p) Else lTotal = rRng.Count ^ p
    End If
End With
ReDim vResult(1 To p)
ReDim vResultAll(1 To lTotal, 1 To p)
 
' Calculate the Combinations / Permutations
Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)
Range("D1").Resize(lTotal, p).Value = vResultAll  'you may adjust for other location
End Sub
 
Sub CombPermNP(ByVal vElements As Variant, ByVal p As Integer, ByVal bComb As Boolean, ByVal bRepet As Boolean, _
                             ByVal vResult As Variant, ByRef lRow As Long, ByRef vResultAll As Variant, ByVal iElement As Integer, ByVal iIndex As Integer)
Dim i As Integer, j As Integer, bSkip As Boolean
 
For i = IIf(bComb, iElement, 1) To UBound(vElements)
    bSkip = False
    ' in case of permutation without repetition makes sure the element is not yet used
    If (Not bComb) And Not bRepet Then
        For j = 1 To p
            If vElements(i) = vResult(j) And Not IsEmpty(vResult(j)) Then
                bSkip = True
                Exit For
            End If
        Next
    End If
 
    If Not bSkip Then
        vResult(iIndex) = vElements(i)
        If iIndex = p Then
            lRow = lRow + 1
            For j = 1 To p
                vResultAll(lRow, j) = vResult(j)
            Next j
        Else
            Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, i + IIf(bComb And bRepet, 0, 1), iIndex + 1)
        End If
    End If
Next i
End Sub
 
Upvote 0
Solution
PS: Your title said combinations (not permutations), so I have assumed that is what you want.

So for example, 1|2 is shown but not 2|1, as that would be the same combination.
 
Upvote 0
Sorry, It was late when I posted. Yes Permutations is what I want. example. 1|2 as well as 2|1

I also should have said 1,2,3,a,b,c,A,B,C,D
 
Last edited:
Upvote 0
For permutations, just change the B2 input cell to FALSE.

But you realise the numbers are going to be much bigger, e.g. 10 billion (10^10) for the permutations of length 10?

Why do you need to enumerate all the possibilities?
 
Upvote 0
@StephenCrump Yes. After looking at the code, I see this may get out of hand quickly. :)

6 was at a million by itself. :( Factors of 10 expand rather quickly.

I may have to scrap the idea I had because it seems it is probably out of Excel's jurisdiction.

Thank you for the code snippet as well as thanks to pgc.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top