PGC, code needs help

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
Office Version
  1. 2010
Hello,

I am working with PGC, code since long time but some how I need help, please can the code can be modified to write output result in a single column separated by "," instead of in multiple columns.

Here below is an example with the code attached how the code output result prints in 4 columns D, E, F &G
I want the result output in a single column "D" separated by coma ",". Like as "A, A, A, A"

*ABCDEFGHI
1P4
2CombinationsFALSE
3RepetitionTRUE
4
5Set Col B5 DownA
6BAAAA
7CAAAB
8AAAC
9AABA
10AABB
11AABC
12AACA
13AACB
14AACC
15ABAA
16ABAB
17ABAC
18ABBA
19ABBB
20ABBC
21ABCA
22ABCB
23ABCC
24ACAA
25ACAB
26ACAC
27ACBA
28ACBB
29ACBC
30ACCA
31ACCB
32ACCC
33BAAA
34BAAB
35BAAC
36BABA
37BABB
38BABC
39BACA
40BACB
41BACC
42BBAA
43BBAB
44BBAC
45BBBA
46BBBB
47BBBC
48BBCA
49BBCB
50BBCC
51BCAA
52BCAB
53BCAC
54BCBA
55BCBB
56BCBC
57BCCA
58BCCB
59BCCC
60CAAA
61CAAB
62CAAC
63CABA
64CABB
65CABC
66CACA
67CACB
68CACC
69CBAA
70CBAB
71CBAC
72CBBA
73CBBB
74CBBC
75CBCA
76CBCB
77CBCC
78CCAA
79CCAB
80CCAC
81CCBA
82CCBB
83CCBC
84CCCA
85CCCB
86CCCC
87
88
89

VBA Code:
'https://www.mrexcel.com/board/threads/combination-help.277924/page-5#post-1424848
'Combination Help
Option Explicit

'bComb=True, bRepet=False - Combinations without repetition
'bComb=True, bRepet=True - Combinations with repetition
'bComb=False, bRepet=False - Permutations without repetition
'bComb=False, bRepet=True - Permutations without repetition
'-------------------------------------------------------------------------------------------------
'http://www.mrexcel.com/forum/excel-questions/277924-combination-help.html

' PGC Set 2007
' Calculates and writes the Combinations / Permutations with/without repetition
' Assumes the result is written from row 1 down. If the total number of cells in a column
' is less than tha number of results continues in another group of columns to the right.
' 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 CombPerm()
Dim rRng As Range, p As Integer
Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
Dim lRow As Long, bComb As Boolean, bRepet As Boolean
Dim vResultPart, iGroup As Integer, l As Long, lMax As Long, k As Long, MaxRow As Long

' Get the inputs and clear the result range (you may adjust for other locations)
Set rRng = Range("B5", Range("B5").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
bComb = Range("B2")
bRepet = Range("B3")
'Range("D1", Cells(1, Columns.Count)).EntireColumn.Clear
Range("D6:IV65536").ClearContents

MaxRow = 65000 'Set Last Row Number For Combination/Permutation To Be Written

' Error
If (Not bRepet) And (rRng.Count < p) Then
    MsgBox "With no repetition the number of elements of the set must be bigger or equal to p"
    Exit Sub
End If

' Set up the arrays for the set elements and the result
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
With Application.WorksheetFunction
    If bComb = True Then
            lTotal = .Combin(rRng.Count + IIf(bRepet, p - 1, 0), p)
    Else
        If bRepet = False Then lTotal = .Permut(rRng.Count, p) Else lTotal = rRng.Count ^ p
    End If
End With
ReDim vResult(1 To p)
ReDim vResultAll(1 To lTotal, 1 To p)

' Calculate the Combinations / Permutations
Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)

' Write the  Combinations / Permutations
' Since writing to the worksheet cell be cell is very slow, uses temporary arrays to write one column at a time
Application.ScreenUpdating = False
If lTotal <= MaxRow Then
    Range("D6").Resize(lTotal, p).Value = vResultAll 'you may adjust for other location
Else
    While iGroup * MaxRow < lTotal
        lMax = lTotal - iGroup * MaxRow
        If lMax > MaxRow Then lMax = MaxRow
        ReDim vResultPart(1 To lMax, 1 To p)
        For l = 1 To lMax
            For k = 1 To p
                vResultPart(l, k) = vResultAll(l + iGroup * MaxRow, k)
            Next k
        Next
        Range("D6").Offset(0, iGroup * (p + 1)).Resize(lMax, p).Value = vResultPart
        iGroup = iGroup + 1
    Wend
End If
Application.ScreenUpdating = True
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

Thank you all.

I am using Excel 2000

Regards,
Moti
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Without re-writing the whole thing, especially since recursion makes my head hurt, this should do the trick.

Book1
ABCD
1P4
2CombinationsFALSE
3RepetitionTRUE
4
5Set Col B5 DownA
6BA,A,A,A
7CA,A,A,B
8A,A,A,C
9A,A,B,A
10A,A,B,B
11A,A,B,C
12A,A,C,A
13A,A,C,B
14A,A,C,C
15A,B,A,A
16A,B,A,B
17A,B,A,C
18A,B,B,A
19A,B,B,B
20A,B,B,C
21A,B,C,A
22A,B,C,B
23A,B,C,C
24A,C,A,A
25A,C,A,B
26A,C,A,C
27A,C,B,A
28A,C,B,B
29A,C,B,C
30A,C,C,A
31A,C,C,B
32A,C,C,C
33B,A,A,A
34B,A,A,B
35B,A,A,C
36B,A,B,A
37B,A,B,B
38B,A,B,C
39B,A,C,A
40B,A,C,B
41B,A,C,C
42B,B,A,A
43B,B,A,B
44B,B,A,C
45B,B,B,A
46B,B,B,B
47B,B,B,C
48B,B,C,A
49B,B,C,B
50B,B,C,C
51B,C,A,A
52B,C,A,B
53B,C,A,C
54B,C,B,A
55B,C,B,B
56B,C,B,C
57B,C,C,A
58B,C,C,B
59B,C,C,C
60C,A,A,A
61C,A,A,B
62C,A,A,C
63C,A,B,A
64C,A,B,B
65C,A,B,C
66C,A,C,A
67C,A,C,B
68C,A,C,C
69C,B,A,A
70C,B,A,B
71C,B,A,C
72C,B,B,A
73C,B,B,B
74C,B,B,C
75C,B,C,A
76C,B,C,B
77C,B,C,C
78C,C,A,A
79C,C,A,B
80C,C,A,C
81C,C,B,A
82C,C,B,B
83C,C,B,C
84C,C,C,A
85C,C,C,B
86C,C,C,C
Sheet1


VBA Code:
Option Explicit

Sub CombPerm()
Dim rRng As Range, p As Integer
Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
Dim lRow As Long, bComb As Boolean, bRepet As Boolean
Dim vResultPart, iGroup As Integer, l As Long, lMax As Long, k As Long, i As Long, MaxRow As Long

' Get the inputs and clear the result range (you may adjust for other locations)
Set rRng = Range("B5", Range("B5").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
bComb = Range("B2")
bRepet = Range("B3")
'Range("D1", Cells(1, Columns.Count)).EntireColumn.Clear
Range("D6:IV65536").ClearContents

MaxRow = 65000 'Set Last Row Number For Combination/Permutation To Be Written

' Error
If (Not bRepet) And (rRng.Count < p) Then
    MsgBox "With no repetition the number of elements of the set must be bigger or equal to p"
    Exit Sub
End If

' Set up the arrays for the set elements and the result
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
With Application.WorksheetFunction
    If bComb = True Then
            lTotal = .Combin(rRng.Count + IIf(bRepet, p - 1, 0), p)
    Else
        If bRepet = False Then lTotal = .Permut(rRng.Count, p) Else lTotal = rRng.Count ^ p
    End If
End With
ReDim vResult(1 To p)
ReDim vResultAll(1 To lTotal, 1 To p)
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")

Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)
Application.ScreenUpdating = False

For i = LBound(vResultAll) To UBound(vResultAll)
    AL.Add Join(Application.Index(vResultAll, i, 0), ",")
Next i
Range("D6").Resize(AL.Count).Value2 = Application.Transpose(AL.toArray)
Application.ScreenUpdating = True
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
Without re-writing the whole thing, especially since recursion makes my head hurt, this should do the trick.
Hello lrobbo314, thank you for answering my query I appreciate your time I am agree I was trying my self to solving this since 2 months but did not get success so finally I thought I must ask MrExcel Experts.

I run the code got error then I create a new workbook but still at the line below code stopped and highlighted it in colour yellow.

VBA Code:
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")

Also I can see that from your side it is showing all the result perfectly in the single column. Please suggest what I am doing wrong.

Good Luck

Kind Regards,
Moti
 
Upvote 0
Means that you don't have VB.Net installed on your computer. Using a dictionary instead of an arraylist should work.

VBA Code:
Option Explicit

Sub CombPerm()
Dim rRng As Range, p As Integer
Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
Dim lRow As Long, bComb As Boolean, bRepet As Boolean
Dim vResultPart, iGroup As Integer, l As Long, lMax As Long, k As Long, i As Long, MaxRow As Long

' Get the inputs and clear the result range (you may adjust for other locations)
Set rRng = Range("B5", Range("B5").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
bComb = Range("B2")
bRepet = Range("B3")
'Range("D1", Cells(1, Columns.Count)).EntireColumn.Clear
Range("D6:IV65536").ClearContents

MaxRow = 65000 'Set Last Row Number For Combination/Permutation To Be Written

' Error
If (Not bRepet) And (rRng.Count < p) Then
    MsgBox "With no repetition the number of elements of the set must be bigger or equal to p"
    Exit Sub
End If

' Set up the arrays for the set elements and the result
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
With Application.WorksheetFunction
    If bComb = True Then
            lTotal = .Combin(rRng.Count + IIf(bRepet, p - 1, 0), p)
    Else
        If bRepet = False Then lTotal = .Permut(rRng.Count, p) Else lTotal = rRng.Count ^ p
    End If
End With
ReDim vResult(1 To p)
ReDim vResultAll(1 To lTotal, 1 To p)
Dim AL As Object: Set AL = CreateObject("Scripting.Dictionary")

Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)
Application.ScreenUpdating = False

For i = LBound(vResultAll) To UBound(vResultAll)
    AL.Add Join(Application.Index(vResultAll, i, 0), ","), True
Next i
Range("D6").Resize(AL.Count).Value2 = Application.Transpose(AL.keys)
Application.ScreenUpdating = True
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
Means that you don't have VB.Net installed on your computer. Using a dictionary instead of an arraylist should work.
lrobbo314, yes true I don't have VB.Net installed on my computer. Your revised code worked nicely, but only changing value in the cell B1 from 4 up to 6 that produced total sets 729 in on column "D" separated by coma. When I changed cell B1 to value 7 it stopped and highlighted the following line...

Please can you check i need at least code work up to max 14 values.

VBA Code:
AL.Add Join(Application.Index(vResultAll, i, 0), ","), True

Good Luck

Kind Regards,
Moti
 
Upvote 0
Not sure why 7 isn't working. It worked on this end. But 14 gives 4,782,969 results and won't fit in a column in modern Excel with ~1 million rows as opposed to 65,536 in Excel 2000.
 
Upvote 0
Not sure why 7 isn't working. It worked on this end.
Hello lrobbo314, I am sure too it works for you for sure; it is my version probably problem.
But 14 gives 4,782,969 results and won't fit in a column in modern Excel with ~1 million rows as opposed to 65,536 in Excel 2000.
With 7 there is 2187, with 8=6561, with 819683, with 9=59049, but it crosses limit 65536 with 10=177147

Instead post#1 version it works with 12 and print 531441 sets but fails with 13 &14.

I am not sure it could be resolved?

I just remember and search again and got the link MickG, resolved here but it is not the same code here is the link below Post#2...


I really appreciate your help and time you spent on it.

Good Luck

Kind Regards,
Moti
 
Upvote 0
Hello, bump

Any idea how to get result in single column using "value14 in cell B1 Post#1" and generating 4,782,969 of sets in my poor version plese help

Kind Regards,
Moti
 
Upvote 0
Got this to work for 14. It obviously can't cram in into 1 column, but it does split the result into 73 columns each with 65536 rows.

Small preview
Book1
ABCDEFGHI
1P14A,A,A,A,A,A,A,A,A,A,A,A,A,AA,A,A,B,A,A,C,C,C,C,A,A,C,BA,A,A,C,A,B,C,C,C,B,A,B,B,CA,A,B,A,A,C,C,C,C,A,A,C,B,AA,A,B,B,B,A,C,C,B,C,B,A,A,BA,A,B,C,B,B,C,C,B,B,B,A,C,C
2CombinationsFALSEA,A,A,A,A,A,A,A,A,A,A,A,A,BA,A,A,B,A,A,C,C,C,C,A,A,C,CA,A,A,C,A,B,C,C,C,B,A,B,C,AA,A,B,A,A,C,C,C,C,A,A,C,B,BA,A,B,B,B,A,C,C,B,C,B,A,A,CA,A,B,C,B,B,C,C,B,B,B,B,A,A
3RepetitionTRUEA,A,A,A,A,A,A,A,A,A,A,A,A,CA,A,A,B,A,A,C,C,C,C,A,B,A,AA,A,A,C,A,B,C,C,C,B,A,B,C,BA,A,B,A,A,C,C,C,C,A,A,C,B,CA,A,B,B,B,A,C,C,B,C,B,A,B,AA,A,B,C,B,B,C,C,B,B,B,B,A,B
4A,A,A,A,A,A,A,A,A,A,A,A,B,AA,A,A,B,A,A,C,C,C,C,A,B,A,BA,A,A,C,A,B,C,C,C,B,A,B,C,CA,A,B,A,A,C,C,C,C,A,A,C,C,AA,A,B,B,B,A,C,C,B,C,B,A,B,BA,A,B,C,B,B,C,C,B,B,B,B,A,C
5Set Col B5 DownAA,A,A,A,A,A,A,A,A,A,A,A,B,BA,A,A,B,A,A,C,C,C,C,A,B,A,CA,A,A,C,A,B,C,C,C,B,A,C,A,AA,A,B,A,A,C,C,C,C,A,A,C,C,BA,A,B,B,B,A,C,C,B,C,B,A,B,CA,A,B,C,B,B,C,C,B,B,B,B,B,A
6BA,A,A,A,A,A,A,A,A,A,A,A,B,CA,A,A,B,A,A,C,C,C,C,A,B,B,AA,A,A,C,A,B,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,C,C,AA,A,A,B,A,A,C,C,C,C,B,A,B,BA,A,A,C,A,B,C,C,C,B,B,B,A,CA,A,B,A,A,C,C,C,C,A,B,C,A,AA,A,B,B,B,A,C,C,B,C,B,C,C,BA,A,B,C,B,B,C,C,B,B,C,A,B,C
26A,A,A,A,A,A,A,A,A,A,A,C,C,BA,A,A,B,A,A,C,C,C,C,B,A,B,CA,A,A,C,A,B,C,C,C,B,B,B,B,AA,A,B,A,A,C,C,C,C,A,B,C,A,BA,A,B,B,B,A,C,C,B,C,B,C,C,CA,A,B,C,B,B,C,C,B,B,C,A,C,A
Sheet1


Not sure if that works for what you're going for, but here goes...

VBA Code:
Public col As Long
Public xDiv As Integer
Option Explicit

'bComb=True, bRepet=False - Combinations without repetition
'bComb=True, bRepet=True - Combinations with repetition
'bComb=False, bRepet=False - Permutations without repetition
'bComb=False, bRepet=True - Permutations without repetition
'-------------------------------------------------------------------------------------------------
'http://www.mrexcel.com/forum/excel-questions/277924-combination-help.html

' PGC Set 2007
' Calculates and writes the Combinations / Permutations with/without repetition
' Assumes the result is written from row 1 down. If the total number of cells in a column
' is less than tha number of results continues in another group of columns to the right.
' 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 CombPerm()
Dim rRng As Range, p As Integer
Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
Dim lRow As Long, bComb As Boolean, bRepet As Boolean
Dim vResultPart, iGroup As Integer, l As Long, lMax As Long, k As Long, MaxRow As Long
col = 1
' Get the inputs and clear the result range (you may adjust for other locations)
Set rRng = Range("B5", Range("B5").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
bComb = Range("B2")
bRepet = Range("B3")
'Range("D1", Cells(1, Columns.Count)).EntireColumn.Clear
Range("D1:IV65536").ClearContents
MaxRow = 65000 'Set Last Row Number For Combination/Permutation To Be Written

' Error
If (Not bRepet) And (rRng.Count < p) Then
    MsgBox "With no repetition the number of elements of the set must be bigger or equal to p"
    Exit Sub
End If

' Set up the arrays for the set elements and the result
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
With Application.WorksheetFunction
    If bComb = True Then
            lTotal = .Combin(rRng.Count + IIf(bRepet, p - 1, 0), p)
    Else
        If bRepet = False Then lTotal = .Permut(rRng.Count, p) Else lTotal = rRng.Count ^ p
    End If
    ReDim vResult(1 To p)
    xDiv = .RoundUp(lTotal / 65536, 0)
    ReDim vResultAll(1 To 65536, 1 To xDiv)
End With


' Calculate the Combinations / Permutations
Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)

' Write the  Combinations / Permutations
' Since writing to the worksheet cell be cell is very slow, uses temporary arrays to write one column at a time
Application.ScreenUpdating = False
Range("D1").Resize(UBound(vResultAll), UBound(vResultAll, 2)).Value = vResultAll 'you may adjust for other location
Application.ScreenUpdating = True
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
            If lRow Mod 65537 = 0 Then
                col = col + 1
                lRow = 1
            End If
            vResultAll(lRow, col) = Join(vResult, ",")
        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
Solution
Got this to work for 14. It obviously can't cram in into 1 column, but it does split the result into 73 columns each with 65536 rows.

Small preview
Book1
ABCDEFGHI
1P14A,A,A,A,A,A,A,A,A,A,A,A,A,AA,A,A,B,A,A,C,C,C,C,A,A,C,BA,A,A,C,A,B,C,C,C,B,A,B,B,CA,A,B,A,A,C,C,C,C,A,A,C,B,AA,A,B,B,B,A,C,C,B,C,B,A,A,BA,A,B,C,B,B,C,C,B,B,B,A,C,C
2CombinationsFALSEA,A,A,A,A,A,A,A,A,A,A,A,A,BA,A,A,B,A,A,C,C,C,C,A,A,C,CA,A,A,C,A,B,C,C,C,B,A,B,C,AA,A,B,A,A,C,C,C,C,A,A,C,B,BA,A,B,B,B,A,C,C,B,C,B,A,A,CA,A,B,C,B,B,C,C,B,B,B,B,A,A
3RepetitionTRUEA,A,A,A,A,A,A,A,A,A,A,A,A,CA,A,A,B,A,A,C,C,C,C,A,B,A,AA,A,A,C,A,B,C,C,C,B,A,B,C,BA,A,B,A,A,C,C,C,C,A,A,C,B,CA,A,B,B,B,A,C,C,B,C,B,A,B,AA,A,B,C,B,B,C,C,B,B,B,B,A,B
4A,A,A,A,A,A,A,A,A,A,A,A,B,AA,A,A,B,A,A,C,C,C,C,A,B,A,BA,A,A,C,A,B,C,C,C,B,A,B,C,CA,A,B,A,A,C,C,C,C,A,A,C,C,AA,A,B,B,B,A,C,C,B,C,B,A,B,BA,A,B,C,B,B,C,C,B,B,B,B,A,C
5Set Col B5 DownAA,A,A,A,A,A,A,A,A,A,A,A,B,BA,A,A,B,A,A,C,C,C,C,A,B,A,CA,A,A,C,A,B,C,C,C,B,A,C,A,AA,A,B,A,A,C,C,C,C,A,A,C,C,BA,A,B,B,B,A,C,C,B,C,B,A,B,CA,A,B,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C,C,A,B,B,C,CA,A,B,B,B,A,C,C,B,C,B,C,C,AA,A,B,C,B,B,C,C,B,B,C,A,B,B
25A,A,A,A,A,A,A,A,A,A,A,C,C,AA,A,A,B,A,A,C,C,C,C,B,A,B,BA,A,A,C,A,B,C,C,C,B,B,B,A,CA,A,B,A,A,C,C,C,C,A,B,C,A,AA,A,B,B,B,A,C,C,B,C,B,C,C,BA,A,B,C,B,B,C,C,B,B,C,A,B,C
26A,A,A,A,A,A,A,A,A,A,A,C,C,BA,A,A,B,A,A,C,C,C,C,B,A,B,CA,A,A,C,A,B,C,C,C,B,B,B,B,AA,A,B,A,A,C,C,C,C,A,B,C,A,BA,A,B,B,B,A,C,C,B,C,B,C,C,CA,A,B,C,B,B,C,C,B,B,C,A,C,A
Sheet1


Not sure if that works for what you're going for, but here goes...

VBA Code:
Public col As Long
Public xDiv As Integer
Option Explicit

'bComb=True, bRepet=False - Combinations without repetition
'bComb=True, bRepet=True - Combinations with repetition
'bComb=False, bRepet=False - Permutations without repetition
'bComb=False, bRepet=True - Permutations without repetition
'-------------------------------------------------------------------------------------------------
'http://www.mrexcel.com/forum/excel-questions/277924-combination-help.html

' PGC Set 2007
' Calculates and writes the Combinations / Permutations with/without repetition
' Assumes the result is written from row 1 down. If the total number of cells in a column
' is less than tha number of results continues in another group of columns to the right.
' 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 CombPerm()
Dim rRng As Range, p As Integer
Dim vElements As Variant, vResult As Variant, vResultAll As Variant, lTotal As Long
Dim lRow As Long, bComb As Boolean, bRepet As Boolean
Dim vResultPart, iGroup As Integer, l As Long, lMax As Long, k As Long, MaxRow As Long
col = 1
' Get the inputs and clear the result range (you may adjust for other locations)
Set rRng = Range("B5", Range("B5").End(xlDown)) ' The set of numbers
p = Range("B1").Value ' How many are picked
bComb = Range("B2")
bRepet = Range("B3")
'Range("D1", Cells(1, Columns.Count)).EntireColumn.Clear
Range("D1:IV65536").ClearContents
MaxRow = 65000 'Set Last Row Number For Combination/Permutation To Be Written

' Error
If (Not bRepet) And (rRng.Count < p) Then
    MsgBox "With no repetition the number of elements of the set must be bigger or equal to p"
    Exit Sub
End If

' Set up the arrays for the set elements and the result
vElements = Application.Index(Application.Transpose(rRng), 1, 0)
With Application.WorksheetFunction
    If bComb = True Then
            lTotal = .Combin(rRng.Count + IIf(bRepet, p - 1, 0), p)
    Else
        If bRepet = False Then lTotal = .Permut(rRng.Count, p) Else lTotal = rRng.Count ^ p
    End If
    ReDim vResult(1 To p)
    xDiv = .RoundUp(lTotal / 65536, 0)
    ReDim vResultAll(1 To 65536, 1 To xDiv)
End With


' Calculate the Combinations / Permutations
Call CombPermNP(vElements, p, bComb, bRepet, vResult, lRow, vResultAll, 1, 1)

' Write the  Combinations / Permutations
' Since writing to the worksheet cell be cell is very slow, uses temporary arrays to write one column at a time
Application.ScreenUpdating = False
Range("D1").Resize(UBound(vResultAll), UBound(vResultAll, 2)).Value = vResultAll 'you may adjust for other location
Application.ScreenUpdating = True
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
            If lRow Mod 65537 = 0 Then
                col = col + 1
                lRow = 1
            End If
            vResultAll(lRow, col) = Join(vResult, ",")
        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
lrobbo314, I tried your latest macro and run it more then 5 times and it work like magic it work with "value14" and got all 4,782,969 sets of combinations less than 10 minutes ?

You’ve done an awesome job! Today after 2 months of time I am feeling relaxed and happy!

I sincerely appreciate your time and the attention needed to resolve this situation.

I wish you Good Luck

Best Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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