Shortcut for Permutations with Repetition

mlarson

Well-known Member
Joined
Aug 25, 2011
Messages
509
Office Version
  1. 2010
Platform
  1. Windows
Hi all, in the excerpt of a code below I am trying to enter all permutations of 4 things. Is there a shortcut to do this without typing out all 256 permutations with repetition (4^4 = 256)? Thanks for your help!

If Col = 1 Then
Ary = Array (1, 1, 1, 1)
ElseIf Col = 2 Then
Ary = Array (1, 1, 1, 1)
ElseIf Col = 3 Then
Ary = Array (1, 1, 1, 1)
ElseIf Col = 4 Then
Ary = Array (1, 2, 3, 4)
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
this will print all combos...

Code:
Sub h()
    Dim r As Long, a As Long, b As Long, c As Long, d As Long
    r = 1
    
    For a = 1 To 4
    For b = 1 To 4
    For c = 1 To 4
    For d = 1 To 4
        Cells(r, 1) = a & b & c & d
        r = r + 1
    Next d
    Next c
    Next b
    Next a
    
End Sub

you can build an array instead of printing, just modify the code
 
Last edited:
Upvote 0
Thanks, cerfani! I'll play around and modify that and let you know if I have any follow-up questions, and will let you know how well it works. Thanks again!
 
Upvote 0
cerfani, how exactly would I modify the code to build an array? I'm a novice when it comes to coding.
 
Upvote 0
Code:
Sub h()
    Dim a As Long, b As Long, c As Long, d As Long
    Dim arrays As New Collection

    For a = 1 To 4
    For b = 1 To 4
    For c = 1 To 4
    For d = 1 To 4
        arrays.Add Array(a, b, c, d)
    Next d
    Next c
    Next b
    Next a

    'all your arrays are in the arrays collection
End Sub

one example, i put all the arrays into another array... you can decide what to do with them but the arrays collection can reference each array using index 0 - 255
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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