10

Click here to load reader

Excel VBA Combinations_Permutations

Embed Size (px)

Citation preview

Page 1: Excel VBA Combinations_Permutations

Forum Question Forums Excel Questions Excel VBA CombinationsPermutations

If this is your first visit be sure to check out the FAQ by clicking the link above You may have to register before you

can post click the register link above to proceed To start viewing messages select the forum that you want to visitfrom the selection below

Become a Registered Member (free) to remove the ad that appears in the top post

Excel VBA CombinationsPermutationsThis is a discussion on Excel VBA CombinationsPermutations within the Excel Questions forums part of the Question Forumscategory Hi Guys Im really struggling to get the answer to the following question and hope you can help I have

User Name Password Log in

Remember Me

RegisterHelp

Search New Posts Zero Reply Posts Subscribed Threads

FAQ Forum Actions Quick Links Advanced Search

Results 1 to 10 of 18 Page 1 of 2 1 2 Last

LinkBack Thread Tools Display

Dec 14th 2009 0731 AM

Excel VBA CombinationsPermutations

Hi Guys

Im really struggling toget the answer to thefollowing question andhope you can help

I have a list with items1 2 and 3

How can I create a listthat gives thefollowing results 1 23 12 13 23 amp 123

I know I can just use3 For loops but I wantthe code to be able tocope with x numbersin the list For example1 2 3 4 and 5 or 12 3 4 5 6 7 8 and9

Any help would begreatly appreciated

1

Join Date

Posts

Dec 2009

9

andrew_sampson

New Member

Share

Reply With Quote

Forum

Dec 14th 2009 0805 AM

Re Excel VBA CombinationsPermutations

Hi AndrewWelcome to the board

You want to calculate the Power Set of a Set

You can try this code

Code

Option Explicit PGC Oct 2007 Calculates a Power Set Set in A1 down Result in C1 down and accross Clears CZSub PowerSet()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd Sub Sub CombinationsNP(vElements As Variant p As Long vresult As Variant lRow As Long iElement As Integer iIndex As Integer)Dim i As Long For i = iElement To UBound(vElements) vresult(iIndex) = vElements(i) If iIndex = p Then lRow = lRow + 1 Range(C amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i + 1 iIndex + 1) End IfNext iEnd Sub

Test

- Write a b c d in A1A4- run PowerSet

A B C D E F G

1 a

2 b a

3 c b

4 d c

5 d

6 a b

7 a c

8 a d

9 b c

10 b d

11 c d

12 a b c

13 a b d

14 a c d

15 b c d

16 a b c d

2

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

17

[Book1]Sheet1

Last edited by pgc01 Dec 14th 2009 at 0809 AM

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 14th 2009 0856 AM

Re Excel VBA CombinationsPermutations

Exactly what i was looking for

Thanks very much pgc01

3

Join Date

Posts

Dec 2009

9

andrew_sampson

New Member

Share

Reply With Quote

Dec 29th 2009 0418 PM

Re Excel VBA CombinationsPermutations

Hello

Im looking for the same thing except for permutationsie orderdoes matter so for 3 s or letters I will get 3^1 + 3^2 + 3^3answers or 39 But just like his original question I dont know howmany Ill have to start with

Thanks

4

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Share

Reply With Quote

Dec 29th 2009 0840 PM

Re Excel VBA CombinationsPermutations

Hi mountainclimber11

5

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Originally Posted by mountainclimber11

Hello

Im looking for the same thing except for permutationsie orderdoes matter so for 3 s or letters I will get 3^1 + 3^2 + 3^3answers or 39 But just like his original question I dont knowhow many Ill have to start with

n + n^2 + n^3 + + n^n

This is a geometric progression the total is

n (n^n - 1) (n - 1)

In the case of 3 elements the total is

3 (3^3 - 1) (3 - 1) = 3 26 2 = 39

The code is similar to the PowerSet but simpler as now you simply

loop through all the values

Insert in a module and run

Code

Option Explicit PGC Dez 2009 Permutations of N elements taken 1 to p at a time Set in A1 down Result in C1 down and accross Clears CZSub PermutationsN_1ToP_R()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call PermutationsNPR(vElements i vresult lRow 1)Next iEnd Sub Sub PermutationsNPR(vElements As Variant p As Long vresult As Variant lRow As Long iIndex As Integer)Dim i As Long For i = 1 To UBound(vElements) vresult(iIndex) = vElements(i) If iIndex = p Then lRow = lRow + 1 Range(C amp lRow)Resize( p) = vresult Else Call PermutationsNPR(vElements p vresult lRow iIndex + 1) End IfNext iEnd Sub

Ex for 3 elements

A B C D E F

1 a a

2 b b

3 c c

4 a a

5 a b

6 a c

7 b a

8 b b

9 b c

10 c a

11 c b

12 c c

13 a a a

14 a a b

15 a a c

16 a b a

17 a b b

18 a b c

19 a c a

20 a c b

21 a c c

22 b a a

23 b a b

24 b a c

25 b b a

26 b b b

27 b b c

28 b c a

29 b c b

30 b c c

31 c a a

32 c a b

33 c a c

34 c b a

35 c b b

36 c b c

37 c c a

38 c c b

39 c c c

40

[Book2]Sheet6

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 30th 2009 1241 PM

Re Excel VBA CombinationsPermutations

Hey thanks My fault I should have said 33 not 39 or 15 setsbecause I dont want the lines that have all the same letter (aaabb cc etc) but your posted answers my question This is what Iended up using

Code

6

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Sub comboze()Dim z y() As String u As Integer v As IntegerDim a As Integer b As Long c As Integer d As IntegerDim w() g ct i j kkz = Array(a b c)u = UBound(z) + 1v = uReDim y(1 To u v 1 To v) w(1 To u v 1 To v)For a = 1 To v For b = 1 To u v Step u a For c = b To b + u (a - 1) - 1 For d = 1 To u y(c + u (a - 1) (d - 1) v - a + 1) = z(d - 1)Next d c b an = u v m = vWith CreateObject(ScriptingDictionary) For i = 1 To n For j = 1 To m Item(y(i j)) = Item(y(i j)) + 1 Next j kk = keys For a = 1 To Count w(i a) = kk(a - 1) Next a removeall Next iFor i = 1 To n g = Empty For j = 1 To m g = g amp Chr(30) amp w(i j) Next j If Not exists(g) Then Add g Empty ct = ct + 1 For j = 1 To m w(ct j) = w(i j) Next j End IfNext iEnd WithRange(A1)Resize(ct m) = wEnd S

From this threadhttpwwwmrexcelcomforumshowthr44post2164844

Thanks

Share

Reply With Quote

Jan 25th 2011 0729 PM

Re Excel VBA CombinationsPermutations

Hi All

This method PowerSet to work the combinations out is certainlyreally impressive

Is it possible to get the PowerSet Code adjusted so it alsoconsiders its own velements

I mean when running the code based on A and B and C

It will returnA B C A B A C B C A B C

I need it to return

A B C A A A B

7

Join Date

Posts

Jan 2011

7

crakonit

New Member

A C B B B C C C A A AA A BA A CA B BA B CA C CB B BB B CB C CC C C

At the moment I can get the desired result by writing eachelements 3 times ie

AAABBBCCC

Once I have the results (by running the powerset code) I removeall the duplicate rows from the results (using excel removeduplicate)

The method works fine until I start increasing the number ofletters simply because excel will not handle so much data Themust be a way to do this without duplicating the elements thenremoving duplicates

Please helpThanks

Share

Reply With Quote

Jan 25th 2011 0754 PM

Re Excel VBA CombinationsPermutations

HiWelcome to the board

With the set of elements in A1 down try

Code

8

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 2: Excel VBA Combinations_Permutations

Dec 14th 2009 0805 AM

Re Excel VBA CombinationsPermutations

Hi AndrewWelcome to the board

You want to calculate the Power Set of a Set

You can try this code

Code

Option Explicit PGC Oct 2007 Calculates a Power Set Set in A1 down Result in C1 down and accross Clears CZSub PowerSet()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd Sub Sub CombinationsNP(vElements As Variant p As Long vresult As Variant lRow As Long iElement As Integer iIndex As Integer)Dim i As Long For i = iElement To UBound(vElements) vresult(iIndex) = vElements(i) If iIndex = p Then lRow = lRow + 1 Range(C amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i + 1 iIndex + 1) End IfNext iEnd Sub

Test

- Write a b c d in A1A4- run PowerSet

A B C D E F G

1 a

2 b a

3 c b

4 d c

5 d

6 a b

7 a c

8 a d

9 b c

10 b d

11 c d

12 a b c

13 a b d

14 a c d

15 b c d

16 a b c d

2

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

17

[Book1]Sheet1

Last edited by pgc01 Dec 14th 2009 at 0809 AM

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 14th 2009 0856 AM

Re Excel VBA CombinationsPermutations

Exactly what i was looking for

Thanks very much pgc01

3

Join Date

Posts

Dec 2009

9

andrew_sampson

New Member

Share

Reply With Quote

Dec 29th 2009 0418 PM

Re Excel VBA CombinationsPermutations

Hello

Im looking for the same thing except for permutationsie orderdoes matter so for 3 s or letters I will get 3^1 + 3^2 + 3^3answers or 39 But just like his original question I dont know howmany Ill have to start with

Thanks

4

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Share

Reply With Quote

Dec 29th 2009 0840 PM

Re Excel VBA CombinationsPermutations

Hi mountainclimber11

5

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Originally Posted by mountainclimber11

Hello

Im looking for the same thing except for permutationsie orderdoes matter so for 3 s or letters I will get 3^1 + 3^2 + 3^3answers or 39 But just like his original question I dont knowhow many Ill have to start with

n + n^2 + n^3 + + n^n

This is a geometric progression the total is

n (n^n - 1) (n - 1)

In the case of 3 elements the total is

3 (3^3 - 1) (3 - 1) = 3 26 2 = 39

The code is similar to the PowerSet but simpler as now you simply

loop through all the values

Insert in a module and run

Code

Option Explicit PGC Dez 2009 Permutations of N elements taken 1 to p at a time Set in A1 down Result in C1 down and accross Clears CZSub PermutationsN_1ToP_R()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call PermutationsNPR(vElements i vresult lRow 1)Next iEnd Sub Sub PermutationsNPR(vElements As Variant p As Long vresult As Variant lRow As Long iIndex As Integer)Dim i As Long For i = 1 To UBound(vElements) vresult(iIndex) = vElements(i) If iIndex = p Then lRow = lRow + 1 Range(C amp lRow)Resize( p) = vresult Else Call PermutationsNPR(vElements p vresult lRow iIndex + 1) End IfNext iEnd Sub

Ex for 3 elements

A B C D E F

1 a a

2 b b

3 c c

4 a a

5 a b

6 a c

7 b a

8 b b

9 b c

10 c a

11 c b

12 c c

13 a a a

14 a a b

15 a a c

16 a b a

17 a b b

18 a b c

19 a c a

20 a c b

21 a c c

22 b a a

23 b a b

24 b a c

25 b b a

26 b b b

27 b b c

28 b c a

29 b c b

30 b c c

31 c a a

32 c a b

33 c a c

34 c b a

35 c b b

36 c b c

37 c c a

38 c c b

39 c c c

40

[Book2]Sheet6

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 30th 2009 1241 PM

Re Excel VBA CombinationsPermutations

Hey thanks My fault I should have said 33 not 39 or 15 setsbecause I dont want the lines that have all the same letter (aaabb cc etc) but your posted answers my question This is what Iended up using

Code

6

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Sub comboze()Dim z y() As String u As Integer v As IntegerDim a As Integer b As Long c As Integer d As IntegerDim w() g ct i j kkz = Array(a b c)u = UBound(z) + 1v = uReDim y(1 To u v 1 To v) w(1 To u v 1 To v)For a = 1 To v For b = 1 To u v Step u a For c = b To b + u (a - 1) - 1 For d = 1 To u y(c + u (a - 1) (d - 1) v - a + 1) = z(d - 1)Next d c b an = u v m = vWith CreateObject(ScriptingDictionary) For i = 1 To n For j = 1 To m Item(y(i j)) = Item(y(i j)) + 1 Next j kk = keys For a = 1 To Count w(i a) = kk(a - 1) Next a removeall Next iFor i = 1 To n g = Empty For j = 1 To m g = g amp Chr(30) amp w(i j) Next j If Not exists(g) Then Add g Empty ct = ct + 1 For j = 1 To m w(ct j) = w(i j) Next j End IfNext iEnd WithRange(A1)Resize(ct m) = wEnd S

From this threadhttpwwwmrexcelcomforumshowthr44post2164844

Thanks

Share

Reply With Quote

Jan 25th 2011 0729 PM

Re Excel VBA CombinationsPermutations

Hi All

This method PowerSet to work the combinations out is certainlyreally impressive

Is it possible to get the PowerSet Code adjusted so it alsoconsiders its own velements

I mean when running the code based on A and B and C

It will returnA B C A B A C B C A B C

I need it to return

A B C A A A B

7

Join Date

Posts

Jan 2011

7

crakonit

New Member

A C B B B C C C A A AA A BA A CA B BA B CA C CB B BB B CB C CC C C

At the moment I can get the desired result by writing eachelements 3 times ie

AAABBBCCC

Once I have the results (by running the powerset code) I removeall the duplicate rows from the results (using excel removeduplicate)

The method works fine until I start increasing the number ofletters simply because excel will not handle so much data Themust be a way to do this without duplicating the elements thenremoving duplicates

Please helpThanks

Share

Reply With Quote

Jan 25th 2011 0754 PM

Re Excel VBA CombinationsPermutations

HiWelcome to the board

With the set of elements in A1 down try

Code

8

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 3: Excel VBA Combinations_Permutations

17

[Book1]Sheet1

Last edited by pgc01 Dec 14th 2009 at 0809 AM

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 14th 2009 0856 AM

Re Excel VBA CombinationsPermutations

Exactly what i was looking for

Thanks very much pgc01

3

Join Date

Posts

Dec 2009

9

andrew_sampson

New Member

Share

Reply With Quote

Dec 29th 2009 0418 PM

Re Excel VBA CombinationsPermutations

Hello

Im looking for the same thing except for permutationsie orderdoes matter so for 3 s or letters I will get 3^1 + 3^2 + 3^3answers or 39 But just like his original question I dont know howmany Ill have to start with

Thanks

4

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Share

Reply With Quote

Dec 29th 2009 0840 PM

Re Excel VBA CombinationsPermutations

Hi mountainclimber11

5

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Originally Posted by mountainclimber11

Hello

Im looking for the same thing except for permutationsie orderdoes matter so for 3 s or letters I will get 3^1 + 3^2 + 3^3answers or 39 But just like his original question I dont knowhow many Ill have to start with

n + n^2 + n^3 + + n^n

This is a geometric progression the total is

n (n^n - 1) (n - 1)

In the case of 3 elements the total is

3 (3^3 - 1) (3 - 1) = 3 26 2 = 39

The code is similar to the PowerSet but simpler as now you simply

loop through all the values

Insert in a module and run

Code

Option Explicit PGC Dez 2009 Permutations of N elements taken 1 to p at a time Set in A1 down Result in C1 down and accross Clears CZSub PermutationsN_1ToP_R()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call PermutationsNPR(vElements i vresult lRow 1)Next iEnd Sub Sub PermutationsNPR(vElements As Variant p As Long vresult As Variant lRow As Long iIndex As Integer)Dim i As Long For i = 1 To UBound(vElements) vresult(iIndex) = vElements(i) If iIndex = p Then lRow = lRow + 1 Range(C amp lRow)Resize( p) = vresult Else Call PermutationsNPR(vElements p vresult lRow iIndex + 1) End IfNext iEnd Sub

Ex for 3 elements

A B C D E F

1 a a

2 b b

3 c c

4 a a

5 a b

6 a c

7 b a

8 b b

9 b c

10 c a

11 c b

12 c c

13 a a a

14 a a b

15 a a c

16 a b a

17 a b b

18 a b c

19 a c a

20 a c b

21 a c c

22 b a a

23 b a b

24 b a c

25 b b a

26 b b b

27 b b c

28 b c a

29 b c b

30 b c c

31 c a a

32 c a b

33 c a c

34 c b a

35 c b b

36 c b c

37 c c a

38 c c b

39 c c c

40

[Book2]Sheet6

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 30th 2009 1241 PM

Re Excel VBA CombinationsPermutations

Hey thanks My fault I should have said 33 not 39 or 15 setsbecause I dont want the lines that have all the same letter (aaabb cc etc) but your posted answers my question This is what Iended up using

Code

6

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Sub comboze()Dim z y() As String u As Integer v As IntegerDim a As Integer b As Long c As Integer d As IntegerDim w() g ct i j kkz = Array(a b c)u = UBound(z) + 1v = uReDim y(1 To u v 1 To v) w(1 To u v 1 To v)For a = 1 To v For b = 1 To u v Step u a For c = b To b + u (a - 1) - 1 For d = 1 To u y(c + u (a - 1) (d - 1) v - a + 1) = z(d - 1)Next d c b an = u v m = vWith CreateObject(ScriptingDictionary) For i = 1 To n For j = 1 To m Item(y(i j)) = Item(y(i j)) + 1 Next j kk = keys For a = 1 To Count w(i a) = kk(a - 1) Next a removeall Next iFor i = 1 To n g = Empty For j = 1 To m g = g amp Chr(30) amp w(i j) Next j If Not exists(g) Then Add g Empty ct = ct + 1 For j = 1 To m w(ct j) = w(i j) Next j End IfNext iEnd WithRange(A1)Resize(ct m) = wEnd S

From this threadhttpwwwmrexcelcomforumshowthr44post2164844

Thanks

Share

Reply With Quote

Jan 25th 2011 0729 PM

Re Excel VBA CombinationsPermutations

Hi All

This method PowerSet to work the combinations out is certainlyreally impressive

Is it possible to get the PowerSet Code adjusted so it alsoconsiders its own velements

I mean when running the code based on A and B and C

It will returnA B C A B A C B C A B C

I need it to return

A B C A A A B

7

Join Date

Posts

Jan 2011

7

crakonit

New Member

A C B B B C C C A A AA A BA A CA B BA B CA C CB B BB B CB C CC C C

At the moment I can get the desired result by writing eachelements 3 times ie

AAABBBCCC

Once I have the results (by running the powerset code) I removeall the duplicate rows from the results (using excel removeduplicate)

The method works fine until I start increasing the number ofletters simply because excel will not handle so much data Themust be a way to do this without duplicating the elements thenremoving duplicates

Please helpThanks

Share

Reply With Quote

Jan 25th 2011 0754 PM

Re Excel VBA CombinationsPermutations

HiWelcome to the board

With the set of elements in A1 down try

Code

8

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 4: Excel VBA Combinations_Permutations

n + n^2 + n^3 + + n^n

This is a geometric progression the total is

n (n^n - 1) (n - 1)

In the case of 3 elements the total is

3 (3^3 - 1) (3 - 1) = 3 26 2 = 39

The code is similar to the PowerSet but simpler as now you simply

loop through all the values

Insert in a module and run

Code

Option Explicit PGC Dez 2009 Permutations of N elements taken 1 to p at a time Set in A1 down Result in C1 down and accross Clears CZSub PermutationsN_1ToP_R()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call PermutationsNPR(vElements i vresult lRow 1)Next iEnd Sub Sub PermutationsNPR(vElements As Variant p As Long vresult As Variant lRow As Long iIndex As Integer)Dim i As Long For i = 1 To UBound(vElements) vresult(iIndex) = vElements(i) If iIndex = p Then lRow = lRow + 1 Range(C amp lRow)Resize( p) = vresult Else Call PermutationsNPR(vElements p vresult lRow iIndex + 1) End IfNext iEnd Sub

Ex for 3 elements

A B C D E F

1 a a

2 b b

3 c c

4 a a

5 a b

6 a c

7 b a

8 b b

9 b c

10 c a

11 c b

12 c c

13 a a a

14 a a b

15 a a c

16 a b a

17 a b b

18 a b c

19 a c a

20 a c b

21 a c c

22 b a a

23 b a b

24 b a c

25 b b a

26 b b b

27 b b c

28 b c a

29 b c b

30 b c c

31 c a a

32 c a b

33 c a c

34 c b a

35 c b b

36 c b c

37 c c a

38 c c b

39 c c c

40

[Book2]Sheet6

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 30th 2009 1241 PM

Re Excel VBA CombinationsPermutations

Hey thanks My fault I should have said 33 not 39 or 15 setsbecause I dont want the lines that have all the same letter (aaabb cc etc) but your posted answers my question This is what Iended up using

Code

6

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Sub comboze()Dim z y() As String u As Integer v As IntegerDim a As Integer b As Long c As Integer d As IntegerDim w() g ct i j kkz = Array(a b c)u = UBound(z) + 1v = uReDim y(1 To u v 1 To v) w(1 To u v 1 To v)For a = 1 To v For b = 1 To u v Step u a For c = b To b + u (a - 1) - 1 For d = 1 To u y(c + u (a - 1) (d - 1) v - a + 1) = z(d - 1)Next d c b an = u v m = vWith CreateObject(ScriptingDictionary) For i = 1 To n For j = 1 To m Item(y(i j)) = Item(y(i j)) + 1 Next j kk = keys For a = 1 To Count w(i a) = kk(a - 1) Next a removeall Next iFor i = 1 To n g = Empty For j = 1 To m g = g amp Chr(30) amp w(i j) Next j If Not exists(g) Then Add g Empty ct = ct + 1 For j = 1 To m w(ct j) = w(i j) Next j End IfNext iEnd WithRange(A1)Resize(ct m) = wEnd S

From this threadhttpwwwmrexcelcomforumshowthr44post2164844

Thanks

Share

Reply With Quote

Jan 25th 2011 0729 PM

Re Excel VBA CombinationsPermutations

Hi All

This method PowerSet to work the combinations out is certainlyreally impressive

Is it possible to get the PowerSet Code adjusted so it alsoconsiders its own velements

I mean when running the code based on A and B and C

It will returnA B C A B A C B C A B C

I need it to return

A B C A A A B

7

Join Date

Posts

Jan 2011

7

crakonit

New Member

A C B B B C C C A A AA A BA A CA B BA B CA C CB B BB B CB C CC C C

At the moment I can get the desired result by writing eachelements 3 times ie

AAABBBCCC

Once I have the results (by running the powerset code) I removeall the duplicate rows from the results (using excel removeduplicate)

The method works fine until I start increasing the number ofletters simply because excel will not handle so much data Themust be a way to do this without duplicating the elements thenremoving duplicates

Please helpThanks

Share

Reply With Quote

Jan 25th 2011 0754 PM

Re Excel VBA CombinationsPermutations

HiWelcome to the board

With the set of elements in A1 down try

Code

8

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 5: Excel VBA Combinations_Permutations

16 a b a

17 a b b

18 a b c

19 a c a

20 a c b

21 a c c

22 b a a

23 b a b

24 b a c

25 b b a

26 b b b

27 b b c

28 b c a

29 b c b

30 b c c

31 c a a

32 c a b

33 c a c

34 c b a

35 c b b

36 c b c

37 c c a

38 c c b

39 c c c

40

[Book2]Sheet6

Kind regardsPGC

To understand recursion you must understand recursion

Share

Reply With Quote

Dec 30th 2009 1241 PM

Re Excel VBA CombinationsPermutations

Hey thanks My fault I should have said 33 not 39 or 15 setsbecause I dont want the lines that have all the same letter (aaabb cc etc) but your posted answers my question This is what Iended up using

Code

6

Join Date

Posts

Dec 2009

60

mountainclimber11

Board Regular

Sub comboze()Dim z y() As String u As Integer v As IntegerDim a As Integer b As Long c As Integer d As IntegerDim w() g ct i j kkz = Array(a b c)u = UBound(z) + 1v = uReDim y(1 To u v 1 To v) w(1 To u v 1 To v)For a = 1 To v For b = 1 To u v Step u a For c = b To b + u (a - 1) - 1 For d = 1 To u y(c + u (a - 1) (d - 1) v - a + 1) = z(d - 1)Next d c b an = u v m = vWith CreateObject(ScriptingDictionary) For i = 1 To n For j = 1 To m Item(y(i j)) = Item(y(i j)) + 1 Next j kk = keys For a = 1 To Count w(i a) = kk(a - 1) Next a removeall Next iFor i = 1 To n g = Empty For j = 1 To m g = g amp Chr(30) amp w(i j) Next j If Not exists(g) Then Add g Empty ct = ct + 1 For j = 1 To m w(ct j) = w(i j) Next j End IfNext iEnd WithRange(A1)Resize(ct m) = wEnd S

From this threadhttpwwwmrexcelcomforumshowthr44post2164844

Thanks

Share

Reply With Quote

Jan 25th 2011 0729 PM

Re Excel VBA CombinationsPermutations

Hi All

This method PowerSet to work the combinations out is certainlyreally impressive

Is it possible to get the PowerSet Code adjusted so it alsoconsiders its own velements

I mean when running the code based on A and B and C

It will returnA B C A B A C B C A B C

I need it to return

A B C A A A B

7

Join Date

Posts

Jan 2011

7

crakonit

New Member

A C B B B C C C A A AA A BA A CA B BA B CA C CB B BB B CB C CC C C

At the moment I can get the desired result by writing eachelements 3 times ie

AAABBBCCC

Once I have the results (by running the powerset code) I removeall the duplicate rows from the results (using excel removeduplicate)

The method works fine until I start increasing the number ofletters simply because excel will not handle so much data Themust be a way to do this without duplicating the elements thenremoving duplicates

Please helpThanks

Share

Reply With Quote

Jan 25th 2011 0754 PM

Re Excel VBA CombinationsPermutations

HiWelcome to the board

With the set of elements in A1 down try

Code

8

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 6: Excel VBA Combinations_Permutations

Sub comboze()Dim z y() As String u As Integer v As IntegerDim a As Integer b As Long c As Integer d As IntegerDim w() g ct i j kkz = Array(a b c)u = UBound(z) + 1v = uReDim y(1 To u v 1 To v) w(1 To u v 1 To v)For a = 1 To v For b = 1 To u v Step u a For c = b To b + u (a - 1) - 1 For d = 1 To u y(c + u (a - 1) (d - 1) v - a + 1) = z(d - 1)Next d c b an = u v m = vWith CreateObject(ScriptingDictionary) For i = 1 To n For j = 1 To m Item(y(i j)) = Item(y(i j)) + 1 Next j kk = keys For a = 1 To Count w(i a) = kk(a - 1) Next a removeall Next iFor i = 1 To n g = Empty For j = 1 To m g = g amp Chr(30) amp w(i j) Next j If Not exists(g) Then Add g Empty ct = ct + 1 For j = 1 To m w(ct j) = w(i j) Next j End IfNext iEnd WithRange(A1)Resize(ct m) = wEnd S

From this threadhttpwwwmrexcelcomforumshowthr44post2164844

Thanks

Share

Reply With Quote

Jan 25th 2011 0729 PM

Re Excel VBA CombinationsPermutations

Hi All

This method PowerSet to work the combinations out is certainlyreally impressive

Is it possible to get the PowerSet Code adjusted so it alsoconsiders its own velements

I mean when running the code based on A and B and C

It will returnA B C A B A C B C A B C

I need it to return

A B C A A A B

7

Join Date

Posts

Jan 2011

7

crakonit

New Member

A C B B B C C C A A AA A BA A CA B BA B CA C CB B BB B CB C CC C C

At the moment I can get the desired result by writing eachelements 3 times ie

AAABBBCCC

Once I have the results (by running the powerset code) I removeall the duplicate rows from the results (using excel removeduplicate)

The method works fine until I start increasing the number ofletters simply because excel will not handle so much data Themust be a way to do this without duplicating the elements thenremoving duplicates

Please helpThanks

Share

Reply With Quote

Jan 25th 2011 0754 PM

Re Excel VBA CombinationsPermutations

HiWelcome to the board

With the set of elements in A1 down try

Code

8

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 7: Excel VBA Combinations_Permutations

A C B B B C C C A A AA A BA A CA B BA B CA C CB B BB B CB C CC C C

At the moment I can get the desired result by writing eachelements 3 times ie

AAABBBCCC

Once I have the results (by running the powerset code) I removeall the duplicate rows from the results (using excel removeduplicate)

The method works fine until I start increasing the number ofletters simply because excel will not handle so much data Themust be a way to do this without duplicating the elements thenremoving duplicates

Please helpThanks

Share

Reply With Quote

Jan 25th 2011 0754 PM

Re Excel VBA CombinationsPermutations

HiWelcome to the board

With the set of elements in A1 down try

Code

8

Join Date

Posts

Apr 2006

14255

pgc01

MrExcel MVP

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 8: Excel VBA Combinations_Permutations

Sub PowerSetRept()Dim vElements As Variant vresult As VariantDim lRow As Long i As Long vElements = ApplicationTranspose(Range(A1 Range(A1)End(xlDown)))Columns(CZ)Clear lRow = 1For i = 1 To UBound(vElements) ReDim vresult(1 To i) Call CombinationsNP(vElements i vresult lRow 1 1)Next iEnd 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 amp lRow)Resize( p) = vresult Else Call CombinationsNP(vElements p vresult lRow i iIndex + 1) End IfNext iEnd Sub

A B C D E F

1 a

2 b a

3 c b

4 c

5 a a

6 a b

7 a c

8 b b

9 b c

10 c c

11 a a a

12 a a b

13 a a c

14 a b b

15 a b c

16 a c c

17 b b b

18 b b c

19 b c c

20 c c c

21

[Book1]Sheet2

Kind regardsPGC

Share

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 9: Excel VBA Combinations_Permutations

laquo Previous Thread | Next Thread raquo

To understand recursion you must understand recursion

Reply With Quote

Jan 25th 2011 0808 PM

Re Excel VBA CombinationsPermutations

Thanks

This is exactly what I was after

Thank you ever so much for doing this =)

9

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Jan 26th 2011 0739 PM

Re Excel VBA CombinationsPermutations

Hi Again

What would be the formula to work out the results of CombReptbased on given no of elements

Say I have 2 elements I need to know (before I run CombRept)how many rows of data will contain 1 element (=2) how many rows of data will contain 2 elements (=3)

Say I have 4 elements I need to know (before I run CombRept) how many rows of data will contain 1 elements(=4)how many rows of data will contain 2 elements(=10)how many rows of data will contain 3 elements (=20) how many rows of data will contain 4 elements (=35)

The build-in COMBIN does really tell the that

Thanks

10

Join Date

Posts

Jan 2011

7

crakonit

New Member

Share

Reply With Quote

Page 1 of 2 1 2 Last

Twitter

Linked In

Google

Reddit

StumbleUpon

Like this thread Share it with others

You may not post newthreads

You may not post replies

You may not postattachments

You may not edit your posts

Posting Permissions

BB code is On

Smilies are On

[IMG] code is On

[VIDEO] code is On

HTML code is On

Trackbacks are On

Pingbacks are On

Refbacks are On

Forum Rules

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA

Page 10: Excel VBA Combinations_Permutations

-- vB4 Default Style Contact Us Ask Mr Excel - Tips and Solutions for Excel Privacy Statement Terms ofService Top

All times are GMT -4 The time now is 0537 PM

Powered by vBulletinreg Version 420 Copyright copy 2014 vBulletin Solutions Inc All rights reserved

All contents Copyright 1998-2014 by MrExcel Consulting

Excel 2007 Excel Formula Excel Help VBA