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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

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