Number Permutations across multiple columns

broncos347

Active Member
Joined
Feb 16, 2005
Messages
291
Office Version
  1. 365
Platform
  1. Windows
I have four columns which I are labelled as Performance (Column A), Safety (Column B), Probability (Column C) and Effect (Column D) and in each one of those columns there could be a number from between 1 and 5.

Is there a formula or a bit of VBA which would be able to generate every permutation of each line of numbers for each of the columns, as shown in the example below?

permutations.xlsm
ABCD
1PerformanceSafetyProbabilityEffect
21111
31211
41221
51231
61241
71251
81222
91223
101224
111225
121232
131233
141234
151235
161233
171234
181235
191242
201243
211244
221245
231252
241253
251254
261255
Sheet1
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Not the fastest but this will work:
VBA Code:
Sub test()
  Dim numbers As Variant, numLen As Integer
  Dim permutation As Long, r As Long, c As Long, i As Long
  numbers = Split("1,2,3,4,5", ",")
  numLen = 4
  permutation = (UBound(numbers) + 1) ^ numLen
  For c = 1 To numLen
    For r = 1 To permutation
      Cells(r, c).Value = numbers(i)
      If r Mod ((UBound(numbers) + 1) ^ (numLen - c)) = 0 Then
        i = IIf(i = 4, 0, i + 1)
      End If
    Next
  Next
End Sub
 
Last edited by a moderator:
Upvote 0
Glad it did work! Thanks for the feedback 👍

EDIT: Even you can modify this line like this
VBA Code:
 i = IIf(i = 4, 0, i + 1)
VBA Code:
i = i Mod (UBound(numbers) + 1)
 
Upvote 0
Hi, here's a formula that you could also try, where the n variable is the numbers to choose from and the c variable is the numbers to choose.

Rich (BB code):
=LET(n,5,c,4,MOD(ROUNDUP(SEQUENCE(n^c)/(n^SEQUENCE(,c,c-1,-1)),0)-1,n)+1)
 
Upvote 0
Last version will look like this:
VBA Code:
Sub test()
  Dim numbers As Variant, permutLen As Integer, numLen As Integer
  Dim permutation As Long, r As Long, c As Long
  numbers = Split("1,2,3,4,5", ",")
  numLen = UBound(numbers) + 1
  permutLen = 4
  permutation = numLen ^ permutLen
  For c = 1 To permutLen
    For r = 1 To permutation
      Cells(r, c).Value = numbers(Int((r - 1) / (permutation / (numLen ^ c))) Mod numLen)
    Next
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

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