You are on page 1of 10

User Name

Password

Log in

Help

Register

Remember Me?

Forum

Search New Posts

FAQ Forum Actions

Forum

Zero Reply Posts

Subscribed Threads
Advanced Search

Quick Links

Question Forums

Excel Questions

Excel VBA Combinations/Permutations

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 visit
from the selection below.

Become a Registered Member (free) to remove the ad that appears in the top post.
Results 1 to 10 of 18

Page 1 of 2 1 2

Last

Excel VBA Combinations/Permutations


This is a discussion on Excel VBA Combinations/Permutations within the Excel Questions forums, part of the Question Forums
category; Hi Guys, I'm really struggling to get the answer to the following question and hope you can help. I have ...
LinkBack

New Member
Join Date:
Posts:

Dec 2009
9

Display

#1

Dec 14th, 2009, 07:31 AM

andrew_sampson

Thread Tools

Excel VBA Combinations/Permutations


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

Share
Reply With Quote

#2

Dec 14th, 2009, 08:05 AM

pgc01

Re: Excel VBA Combinations/Permutations

MrExcel MVP
Join Date:
Posts:

Apr 2006
14,255

Hi Andrew
Welcome to the board
You want to calculate the Power Set of a Set.
You can try this code:
Code:
Oto Epii
pin xlct
'PCOt20
G c 07
'Cluae aPwrSt
aclts
oe e
'Sti A,dw.Rsl i C,dw adacos Ce
e n 1 on eut n 1 on n crs. l
asCZ
r :.
SbPwre(
u oeSt)
DmvlmnsA Vrat veutA Vrat
i Eeet s ain, rsl s ain
Dmlo A Ln,iA Ln
i Rw s og
s og
vlmns=ApiainTasoeRne"1,Rne"
Eeet
plcto.rnps(ag(A" ag(A
1)EdxDw))
".n(lon)
Clms":".la
oun(CZ)Cer
lo =1
Rw
Fri=1T Uon(Eeet)
o
o Budvlmns
RDmveut1T i
ei rsl( o )
Cl CmiainN(Eeet,i veut lo,1
al obntosPvlmns , rsl, Rw
,1
)
Nx i
et
EdSb
n u
SbCmiainN(Eeet A Vrat pA Ln,v
u obntosPvlmns s ain,
s og r
eutA Vrat lo A Ln,ilmn A Itgr i
sl s ain, Rw s og Eeet s nee,
IdxA Itgr
ne s nee)
DmiA Ln
i
s og
Fri=ilmn T Uon(Eeet)
o
Eeet o Budvlmns
veutine)=vlmnsi
rsl(Idx
Eeet()
I ine =pTe
f Idx
hn
lo =lo +1
Rw
Rw
Rne""&lo)Rsz( p =veut
ag(C
Rw.eie, )
rsl
Es
le
Cl obntosPvlmns , rsl, R
Test:, i + 1 a l C m i a i n N ( E e e t , p v e u t l o
w
,ine +1
Idx
)
EdI
n f
N xa, b,
e
- Write t i c, d in A1:A4
EPowerSet
d u
- run n S b

b
c

10

11

12

13

14

15

16

17
[Book1]Sheet1

Last edited by pgc01; Dec 14th, 2009 at 08:09 AM.


Share

Kind regards
PGC
To understand recursion, you must understand recursion.
Reply With Quote

#3

Dec 14th, 2009, 08:56 AM

andrew_sampson
New Member
Join Date:
Posts:

Dec 2009
9

Re: Excel VBA Combinations/Permutations


Exactly what i was looking for!!
Thanks very much pgc01

Share
Reply With Quote

#4

Dec 29th, 2009, 04:18 PM

mountainclimber11
Board Regular
Join Date:
Posts:

Dec 2009
60

Re: Excel VBA Combinations/Permutations


Hello,
I'm looking for the same thing except for permutations...ie order
does matter so for 3 #'s or letters I will get 3^1 + 3^2 + 3^3
answers, or 39. But just like his original question, I don't know how
many I'll have to start with.
Thanks!

Share
Reply With Quote

#5

Dec 29th, 2009, 08:40 PM

pgc01
MrExcel MVP
Join Date:
Posts:

Apr 2006
14,255

Re: Excel VBA Combinations/Permutations


Originally Posted by mountainclimber11

Hello,
I'm looking for the same thing except for permutations...ie order
does matter so for 3 #'s or letters I will get 3^1 + 3^2 + 3^3
answers, or 39. But just like his original question, I don't know
how many I'll have to start with.

Hi mountainclimber11

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:
Oto Epii
pin xlct
'PCDz20
G e 09
'Pruain o Neeet tkn1t pa atm
emttos f
lmns ae
o
t
ie
'Sti A,dw.Rsl i C,dw adacos Ce
e n 1 on eut n 1 on n crs. l
asCZ
r :.
SbPruainN1o_(
u emttos_TPR)
DmvlmnsA Vrat veutA Vrat
i Eeet s ain, rsl s ain
Dmlo A Ln,iA Ln
i Rw s og
s og
vlmns=ApiainTasoeRne"1,Rne"
Eeet
plcto.rnps(ag(A" ag(A
1)EdxDw))
".n(lon)
Clms":".la
oun(CZ)Cer
Fri=1T Uon(Eeet)
o
o Budvlmns
RDmveut1T i
ei rsl( o )
Cl PruainNRvlmns i veut lo,
al emttosP(Eeet, , rsl, Rw
1
)
Nx i
et
EdSb
n u
SbPruainNRvlmnsA Vrat pA Ln,v
u emttosP(Eeet s ain,
s og
rsl A Vrat lo A Ln,ine A Itgr
eut s ain, Rw s og Idx s nee)
DmiA Ln
i
s og
Fri=1T Uon(Eeet)
o
o Budvlmns
veutine)=vlmnsi
rsl(Idx
Eeet()
I ine =pTe
f Idx
hn
lo =lo +1
Rw
Rw
Rne""&lo)Rsz( p =veut
ag(C
Rw.eie, )
rsl
Es
le
Cl PruainNRvlmns p veut l
al emttosP(Eeet, , rsl, R
o,ine +1
w Idx
)
n f
Ex, forx E d I
3 elements:
N ti
e
EdSb
n u

a
b

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40
[Book2]Sheet6

Share

Kind regards
PGC
To understand recursion, you must understand recursion.
Reply With Quote

#6

Dec 30th, 2009, 12:41 PM

mountainclimber11
Board Regular
Join Date:
Posts:

Dec 2009
60

Re: Excel VBA Combinations/Permutations


Hey, thanks! My fault, I should have said 33 not 39 or 15 sets
because I don't want the lines that have all the same letter (aaa,
bb, cc, etc.), but your posted answers my question. This is what I
ended up using:
Code:

Sbcmoe)
u obz(
Dmz y)A Srn,uA Itgr vA Itgr
i , ( s tig
s nee,
s nee
DmaA Itgr bA Ln,cA Itgr dA Itg
i
s nee,
s og
s nee,
s nee
r
Dmw) g c,i j k
i (, , t , , k
z=Ary"" "" ""
ra(a, b, c)
u=Uon()+1
Budz
v=u
RDmy1T u^v 1T v,w1T u^v 1T v
ei ( o
,
o ) ( o
,
o )
Fra=1T v
o
o
Frb=1T u^vSe u^a
o
o
tp
Frc=bT b+u^( -1 -1
o
o
a
)
Frd=1T u
o
o
yc+u^( -1 *( -1,v-a+1
(
a
)
d
)
)=zd-1
(
)
Nx d c b a
et , , ,
n=u^v m=v
:
Wt CetOjc(SrpigDcinr"
ih raebet"citn.itoay)
Fri=1T n
o
o
Frj=1T m
o
o
.tmyi j)=.tmyi j)+1
Ie((, )
Ie((, )
Nx j
et
k =.es
k
ky
Fra=1T .on
o
o Cut
wi a =k( -1
(, )
ka
)
Nx a
et
.eoel
rmval
Nx i
et
Fri=1T n
o
o
g=Epy
mt
Frj=1T m g=g&Cr3)&wi j:Nx j
o
o :
h(0
(, ) et
I Nt.xssg Te
f o eit() hn
.d g Epy
Ad , mt
c =c +1
t
t
Frj=1T m wc,j =wi j:Nx j
o
o : (t )
(, ) et
From this n I
Ethread:
d f
Nx i
et
http://www.mrexcel.com/forum/showthr...44#post2164844
EdWt
n ih
Rne"1)Rsz(t m =w
ag(A".eiec, )
Thanks! S
Ed
n

Share
Reply With Quote

#7

Jan 25th, 2011, 07:29 PM

crakonit

Re: Excel VBA Combinations/Permutations

New Member
Join Date:
Posts:

Jan 2011
7

Hi All,
This method "PowerSet" to work the combinations out is certainly
really impressive!
Is it possible to get the PowerSet Code adjusted so it also
considers its own velements?.
I mean, when running the code based on 'A' and 'B' and 'C'
It will return:
A
B
C
AB
AC
BC
ABC

I need it to return:
A
B
C
AA
AB

A
B
B
C
A
A
A
A
A
A
B
B
B
C

C
B
C
C
AA
AB
AC
BB
BC
CC
BB
BC
CC
CC

At the moment I can get the desired result by writing each


elements 3 times, ie:
A
A
A
B
B
B
C
C
C
Once I have the results (by running the powerset code), I remove
all the duplicate rows from the results (using excel 'remove
duplicate').
The method works fine until I start increasing the number of
letters, simply because excel will not handle so much data. The
must be a way to do this without duplicating the elements then
removing duplicates.
Please help!
Thanks

Share
Reply With Quote

#8

Jan 25th, 2011, 07:54 PM

pgc01
MrExcel MVP
Join Date:
Posts:

Apr 2006
14,255

Re: Excel VBA Combinations/Permutations


Hi
Welcome to the board
With the set of elements in A1, down, try:
Code:

SbPwreRp(
u oeStet)
DmvlmnsA Vrat veutA Vrat
i Eeet s ain, rsl s ain
Dmlo A Ln,iA Ln
i Rw s og
s og
vlmns=ApiainTasoeRne"1,Rne"
Eeet
plcto.rnps(ag(A" ag(A
1)EdxDw))
".n(lon)
Clms":".la
oun(CZ)Cer
lo =1
Rw
Fri=1T Uon(Eeet)
o
o Budvlmns
RDmveut1T i
ei rsl( o )
Cl CmiainN(Eeet,i veut lo,1
al obntosPvlmns , rsl, Rw
,1
)
Nx i
et
EdSb
n u
SbCmiainN(Eeet A Vrat pA Ln,v
u obntosPvlmns s ain,
s og r
eutA Vrat lo A Ln,ilmn A Ln,in
sl s ain, Rw s og Eeet s og Id
e A Ln)
x s og
DmiA Ln
i
s og
Fri=ilmn T Uon(Eeet)
o
Eeet o Budvlmns
veutine)=vlmnsi
rsl(Idx
Eeet()
I ine =pTe
f Idx
hn
lo =lo +1
Rw
Rw
Rne""&lo)Rsz( p =veut
ag(C
Rw.eie, )
rsl
Es
le
Cl CmiainN(Eeet,p veut lo
al obntosPvlmns , rsl, R
w i ine +1
, , Idx
)
EdI
n f
Nx i
et
EdSb
n u

b
c

10

11

12

13

14

15

16

17

18

19

20

21
[Book1]Sheet2

Share

Kind regards
PGC

To understand recursion, you must understand recursion.


Reply With Quote

#9

Jan 25th, 2011, 08:08 PM

crakonit

Re: Excel VBA Combinations/Permutations

New Member
Join Date:
Posts:

Jan 2011
7

Thanks.
This is exactly what I was after!
Thank you ever so much for doing this =)

Share
Reply With Quote

#10

Jan 26th, 2011, 07:39 PM

crakonit

Re: Excel VBA Combinations/Permutations

New Member
Join Date:
Posts:

Jan 2011
7

Hi Again...
What would be the formula to work out the results of CombRept
based 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!

Share
Reply With Quote

Page 1 of 2 1 2

Last

Previous Thread | Next Thread


Like this thread? Share it with others

Twitter
Linked In
Google
Reddit
StumbleUpon

Posting Permissions

You may not post new


threads
You may not post replies
You may not post
attachments
You may not edit your posts

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

Excel 2007
-- vB4 Default Style

Excel Formula

Excel Help

VBA

Contact Us Ask Mr. Excel - Tips and Solutions for Excel Privacy Statement Terms of
Service Top
All times are GMT -4. The time now is 05:37 PM.
Powered by vBulletin Version 4.2.0
Copyright 2014 vBulletin Solutions, Inc. All rights reserved.
All contents Copyright 1998-2014 by MrExcel Consulting.

You might also like