unique combinations generator

mikiel111

New Member
Joined
Mar 17, 2020
Messages
38
Office Version
  1. 365
Hi there,

I`ve got this issue which i just cannot seem to get to the bottom of.
I need a unique combination generator from entries in 1 column. To elaborate, I have 1 column with any number of rows (A, B, C, D etc...) in sheet A & I need to output unique combinations of all the entries from that column in groups of 5 in Sheet B
So let`s say I have Column 1 with A, B, C, D & E in the rows. I need to output the unique combinations of those (e.g. AAAAA AAAAB AAAAC, AAAAD, AAABC, AAABD etc...). The order of the letters is not important, so tby that I mean if I have AAAAB I dont want AAABA, AABAA, ABAAA & BAAAA.
I found an excel file on the internet which does what seems like I need it to do but the knowledge behind it is waaaaayy beyond me (hence why i`m reaching out for help) so I cannot copy it, see whats happening or recreate it in my own file.

Is there some way I can attach it so you see what I am talking about?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi and welcome to MrExcel.

Just to understand how you have your data.

They are like this:
varios
A
1AAABA
2AAAAB
3BAAAA
4AABAA
Sheet A


And you want this result:
varios
A
1AAAAB
Sheet B


How many records do you have on sheet A?
 
Upvote 0
Welcome to the Forum!

Workbook here: Box

Using general code (thanks pgc01) for combinations/permutations with/without repetition here: Combination, Repeats (AAB,BBC) allowed, Repeats (AAC,ACA,CAA) only considered as One

Results:

Combinations.xlsm
ABCDEFGH
1A5AAAAA
2BAAAAB
3CAAAAC
4DAAAAD
5EAAAAE
6AAABB
7AAABC
8AAABD
9AAABE
10AAACC
11AAACD
12AAACE
13AAADD
14AAADE
15AAAEE
16AABBB
17AABBC
18AABBD
19AABBE
20AABCC
21AABCD
22AABCE
23AABDD
24AABDE
25AABEE
26AACCC
27etc
Sheet1
 
Upvote 0
Welcome to the Forum!

Workbook here: Box

Using general code (thanks pgc01) for combinations/permutations with/without repetition here: Combination, Repeats (AAB,BBC) allowed, Repeats (AAC,ACA,CAA) only considered as One

Results:

Combinations.xlsm
ABCDEFGH
1A5AAAAA
2BAAAAB
3CAAAAC
4DAAAAD
5EAAAAE
6AAABB
7AAABC
8AAABD
9AAABE
10AAACC
11AAACD
12AAACE
13AAADD
14AAADE
15AAAEE
16AABBB
17AABBC
18AABBD
19AABBE
20AABCC
21AABCD
22AABCE
23AABDD
24AABDE
25AABEE
26AACCC
27etc
Sheet1

I`m doing my best to stay away from code as I know nothing about coding and if i want even the smallest thing changed i need to stay bugging people to help me.
I will explore however this. In the link you posted do I need to look at post #6?
 
Upvote 0
Hi and welcome to MrExcel.

Just to understand how you have your data.

They are like this:
varios
A
1AAABA
2AAAAB
3BAAAA
4AABAA
Sheet A


And you want this result:
varios
A
1AAAAB
Sheet B


How many records do you have on sheet A?

My data is the single column on the far right (entries A, B, C etc..) and I wanted the final output as shown int he coloured columns (so in column, B, D, F, H & J)

Untitled.png
 
Upvote 0
I`m doing my best to stay away from code as I know nothing about coding and if i want even the smallest thing changed i need to stay bugging people to help me.
I will explore however this. In the link you posted do I need to look at post #6?
Do I just copy paste his VBA code and make it into a macro? I mean it`s like you have someone that needs to fly to another country and you`ve given me a plane and said ... here fly it..... errrr i`m not a pilot
 
Upvote 0
Ok, I think i`m getting somewhere. I have copied the module from your workbook stephencrump to my excel sheet. However I have my list of items in Column B starting at cell 10 (this is in sheet A) and I need my output in sheet B in columns D, F, H, J, L (starting from row 2). (so to live nothing to the imagination first output in D2, F2, H2, J2, L2, next output in D3, F3 etc....

What should i change from below to achieve the above?

VBA Code:
' PGC Set 2007
' Calculates and writes the Combinations / Permutations with/without repetition
' vElements - Array with the set elements (1 to n)
' p - number of elements in 1 combination/permutation
' bComb - True: Combinations, False: Permutations
' bRepet - True: with repetition, False: without repetition
' vResult - Array to hold 1 permutation/combination (1 to p)
' lRow - row number. the next combination/permutation is written in lRow+1
' vResultAll - Array to hold all the permutations/combinations (1 to Total, 1 to p)
' iElement - order of the element to process in case of combination
' iIndex - position of the next element in the combination/permutation
' Sub CombPerm() deals with the input / output
' Sub CombPermNP() generates the combinations / permutations
Sub Go()

    Dim vElements As Variant, vResult As Variant, vResultAll As Variant
    Dim lrow As Long, lTotal As Long, p As Long
    Dim bComb As Boolean, bRepet As Boolean
    Dim rng As Range
    
    p = Range("N").Value
    bComb = True
    bRepet = True
    Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    vElements = Application.Index(Application.Transpose(rng), 1, 0)
    
    With Application
        If bComb Then
            lTotal = .Combin(UBound(vElements) + IIf(bRepet, p - 1, 0), p)
        Else
            If bRepet = False Then lTotal = .Permut(UBound(vElements), p) Else lTotal = UBound(vElements) ^ p
        End If
    End With
    ReDim vResult(1 To p)
    ReDim vResultAll(1 To lTotal, 1 To p)

    Call CombPermNP(vElements, p, True, True, vResult, lrow, vResultAll, 1, 1)
    
    With Range("D1")
        .CurrentRegion.ClearContents
        .Resize(lTotal, p).Value = vResultAll
    End With
    
End Sub
Sub CombPermNP(ByVal vElements As Variant, ByVal p As Integer, ByVal bComb As Boolean, ByVal bRepet As Boolean, _
                             ByVal vResult As Variant, ByRef lrow As Long, ByRef vResultAll As Variant, ByVal iElement As Integer, ByVal iIndex As Integer)
Dim i As Integer, j As Integer, bSkip As Boolean
 
For i = IIf(bComb, iElement, 1) To UBound(vElements)
    bSkip = False
    ' in case of permutation without repetition makes sure the element is not yet used
    If (Not bComb) And Not bRepet Then
        For j = 1 To p
            If vElements(i) = vResult(j) And Not IsEmpty(vResult(j)) Then
                bSkip = True
                Exit For
            End If
        Next
    End If
 
    If Not bSkip Then
        vResult(iIndex) = vElements(i)
        If iIndex = p Then
            lrow = lrow + 1
            For j = 1 To p
                vResultAll(lrow, j) = vResult(j)
            Next j
        Else
            Call CombPermNP(vElements, p, bComb, bRepet, vResult, lrow, vResultAll, i + IIf(bComb And bRepet, 0, 1), iIndex + 1)
        End If
    End If
Next i
End Sub
 
Upvote 0
I may also sometimes have to create combinations which are less than 4. Is this achievable with that code?

Also sorry for the millions of psots, i`m used to forum where i can edit my posts
 
Upvote 0
I'm happy to make the changes to the code, and I will point you to the relevant code lines so that you can understand which lines you might want to change subsequently. But let's be clear about the specifications, so I only need to do it once:

- Is the code generating the results you want (combinations with repetition)?

- List of items starting in 'Sheet A'B10? (You said M2 in Post #5?) Can the code assume there will be nothing in all cells below the list?

- Output starting in 'Sheet A' D2, F2, H2, J2, L2. (In Post #5, you said column, B, D, F, H & J?) Can I assume no used cells below these output columns?

The code will generate combination of N, where N is 1,2,3,4,5 ... Is 5 the maximum number you will need. Or should we allow for more columns N2, P2 ...
 
Upvote 0
Thank you very much! I reviewed my excel sheet layout & changed the columns. Instead of working with strings I think it`s better we work with their IDs, simple numbers. If it is not easier tell me so I`ll change stuff around.

- The sheet you shared with me outputs the exact results that I want (so if i have AAAAB, I dont want AAABA, AABAA, ABAAA or BAAAA).

- My list of items (source data) starts in a sheet called Meal List. The first entry will be in A10, second entry in A11, third in A12 etc... These are the IDs for my entries which will be held adjcent in column B (so A10 has the description in B10). The IDs (again held in the A column) are as simple as can be; first ID is 1, second ID is 2 etc... Yes, the code can assume there is nothing below the list. Just in case it is important to know in the same row there things related to that same entry (the description in column B for example). Above the list there will be things and I may need to shift the rows down to add stuff. So if my data started from row 10 now it starts from row 11 as I would have added a row above for whatever reason.

- The output is in a different sheet called Daily Meal Macro. Yes you can assume no used cells below the output however once again i may put stuff above it. The output columns are C, E, G, I & K. If we make 6 combinations then the 6th column would be M. The output should start from second row -> C2, E2, G2, I2, K2 (& M2 if we can go up to 6 combinations)

- 5 is sufficient, if it is no hassle to make it take up to 6 that would be great but it`s a "nice to have" not a "must have". I`m highly appreciative of what youre doing

So basically the combinations outputted will be numbers (11111, 11112, 11113 etc....), like that I can use match/index or lookups to load corresponding data. Does this make it easier?
Is it OK to have the option to add/remove stuff (hence rows) above the input/output rows?
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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