Excel VBA Combinations/Permutations

andrew_sampson

New Member
Joined
Dec 14, 2009
Messages
9
Hi Guys,

I'm really struggling to get the answer to the following question and hope you can help.

I have a list with items 1, 2 and 3.

How can I create a list that gives the following results: 1, 2, 3, 12, 13, 23 & 123

I know I can just use 3 For loops but I want the code to be able to cope with x numbers in the list. For example 1, 2, 3, 4 and 5 or 1, 2, 3, 4, 5, 6, 7, 8 and 9

Any help would be greatly appreciated
 
I think this is correct:

Code:
      -A- --B--- --C--- --D--- --E--- --F---
  1   Set Univ=2 Univ=3 Univ=4 Univ=5 Univ=6
  2     1      2      3      4      5      6
  3     2      3      6     10     15     21
  4     3      4     10     20     35     56
  5     4      5     15     35     70    126
  6     5      6     21     56    126    252
  7     6      7     28     84    210    462

The formula in B2 and copied across and down is

=COMBIN($A2+B$1-1, B$1-1)

The "Univ=" in row 1 is just formatting.
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi!

Thanks for the great piece of code it was (almost) exactly what I was looking for! But, (there's always a but ;)) as I need to create very large power sets (for sets with hundreds of elements) the limited number of rows in Excel become a problem. Furthermore I only need the subsets with two or more elements. I use these subsets to solve Capacitated Vehicle Routing Problems and in order to easily include the sets to my optimization code I would like a specific format on the output:
For the set S{1,2,3}
I would want to get this output:
1.(1,2)
2.(1,3)
3.(2,3)
4.(1,2,3)
Where the first number is a counter for the subsets.

Is there any possibility you could modify the code so that it writes the output to a file, for instance txt, in the above format. I tried to modify the code myself but as I'm very unfamiliar with VBA I didn't get anywhere.

Thanks for your help,
Mic_kas
 
Upvote 0
Hi there:

Sorry to ask this silly question, i am not really good on VBA coding, i would like to create a sequence of permutation giving in excel at the next horizontal columnes (at the same line) a closed ring of numbers by preserving the native order, exemple: 12345 - 51234 - 45123 - 34512 - 23451, so the whole permutation can be included in the same case in order to apply additional functions bellow.


Thank you for your help.
 
Upvote 0
IF A1=1, B1=2, C1=3, D1=4, E1=5
put
=IF(COUNTA($1:$1) < COLUMN(A1), "", INDEX($1:$1, 1, 1+MOD(COLUMN(A1)-ROW(A1)-1, COUNTA($1:$1))))

in a cell and drag down and right
 
Upvote 0
Thank you so much.

An other question, how can i keep the first zero of each number i put without setting the mask to some special mask (like 000000 or 0000) because the numbers length is different from each others (example: 0165895325, 09865842, 048 ...).
i think i need a macro code no?
 
Upvote 0
I'm not sure what you are asking. If you want an initial character in the result,

=IF(COUNTA($1:$1) < COLUMN(A1), "", "0" & INDEX($1:$1, 1, 1+MOD(COLUMN(A1)-ROW(A1)-1, COUNTA($1:$1))))

If the problem is that you have strings and the formula is converting them to numbers =theformula & "" will fix that.
 
Upvote 0
The command you gave me previously didn't work with my french excel version even i've translated the commands.
i've used:

=MOD(A1;10)*10^(NBCHAR(A2)-1)+(A2-MOD(A2;10))/10

And then i dragged it;

For the numbers without 0 there is no problem, the problem is when a number contains 0 so in the rotation process, the 0 disappears, so i want to use a command line or a macro to "fix" the zero when it returns at the beginning of the number.

Thank you mikerickson;
 
Upvote 0
Hi
Welcome to the board

With the set of elements in A1, down, try:

Code:
Sub PowerSetRept()
Dim vElements As Variant, vresult As Variant
Dim lRow As Long, i As Long
 
vElements = Application.Transpose(Range("A1", Range("A1").End(xlDown)))
Columns("C:Z").Clear
 
lRow = 1
For i = 1 To UBound(vElements)
    ReDim vresult(1 To i)
    Call CombinationsNP(vElements, i, vresult, lRow, 1, 1)
Next i
End Sub
 
Sub CombinationsNP(vElements As Variant, p As Long, vresult As Variant, lRow As Long, iElement As Long, iIndex As Long)
Dim i As Long
 
For i = iElement To UBound(vElements)
    vresult(iIndex) = vElements(i)
    If iIndex = p Then
        lRow = lRow + 1
        Range("C" & lRow).Resize(, p) = vresult
    Else
        Call CombinationsNP(vElements, p, vresult, lRow, i, iIndex + 1)
    End If
Next i
End Sub


ABCDEF
1a
2ba
3cb
4c
5aa
6ab
7ac
8bb
9bc
10cc
11aaa
12aab
13aac
14abb
15abc
16acc
17bbb
18bbc
19bcc
20ccc
21
[Book1]Sheet2

<tbody>
</tbody>

I am looking for something similar:
ABCDE
1a
2ba
3cb
4c
5ab
6ba
7ac
8ca
9bc
cb
abc
cba
...

<tbody>
</tbody>

Basically a list of all combinations. Thank you for your help.
 
Upvote 0
Hi

Looking at the example you posted you don't want the list of all the combinations, or else you would not have posted both (a,c) and (c,a) which are the same combination.

I guess you mean a list of all the permutations.

Try my post #2, where I posted the solution to the powerset, replacing the statement:

Code:
For i = iElement To UBound(vElements)

with

Code:
For i = 1 To UBound(vElements)
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

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