+ Reply to Thread
Results 1 to 36 of 36

VBA code for Permutation and combination

Hybrid View

saravanan2021 VBA code for Permutation and... 08-19-2022, 02:25 AM
kvsrinivasamurthy Re: VBA code for Permutation... 08-19-2022, 03:03 AM
saravanan2021 Re: VBA code for Permutation... 08-19-2022, 03:36 AM
Trebor76 Re: VBA code for Permutation... 08-19-2022, 05:04 AM
saravanan2021 Re: VBA code for Permutation... 08-25-2022, 09:47 PM
JEC. Re: VBA code for Permutation... 08-19-2022, 05:06 AM
JohnTopley Re: VBA code for Permutation... 08-19-2022, 07:56 AM
JEC. Re: VBA code for Permutation... 08-19-2022, 09:55 AM
Trebor76 Re: VBA code for Permutation... 08-19-2022, 06:46 PM
JEC. Re: VBA code for Permutation... 08-19-2022, 07:41 PM
saravanan2021 Re: VBA code for Permutation... 08-25-2022, 09:53 PM
Trebor76 Re: VBA code for Permutation... 08-19-2022, 09:51 PM
JohnTopley Re: VBA code for Permutation... 08-20-2022, 02:48 AM
bebo021999 Re: VBA code for Permutation... 08-26-2022, 12:46 AM
JEC. Re: VBA code for Permutation... 08-26-2022, 02:50 AM
saravanan2021 Re: VBA code for Permutation... 08-26-2022, 03:10 AM
JEC. Re: VBA code for Permutation... 08-26-2022, 03:15 AM
bsalv Re: VBA code for Permutation... 08-29-2022, 06:15 AM
saravanan2021 Re: VBA code for Permutation... 08-30-2022, 05:17 AM
bsalv Re: VBA code for Permutation... 08-30-2022, 05:40 AM
HansDouwe Re: VBA code for Permutation... 09-03-2022, 04:27 PM
saravanan2021 Re: VBA code for Permutation... 09-03-2022, 04:27 AM
bsalv Re: VBA code for Permutation... 09-03-2022, 11:37 AM
HansDouwe Re: VBA code for Permutation... 09-03-2022, 02:43 PM
bsalv Re: VBA code for Permutation... 09-03-2022, 02:55 PM
HansDouwe Re: VBA code for Permutation... 09-03-2022, 03:18 PM
bsalv Re: VBA code for Permutation... 09-03-2022, 03:34 PM
mikerickson Re: VBA code for Permutation... 09-03-2022, 03:47 PM
HansDouwe Re: VBA code for Permutation... 09-03-2022, 04:13 PM
saravanan2021 Re: VBA code for Permutation... 09-05-2022, 05:20 AM
bsalv Re: VBA code for Permutation... 09-03-2022, 05:10 PM
HansDouwe Re: VBA code for Permutation... 09-03-2022, 06:15 PM
bsalv Re: VBA code for Permutation... 09-04-2022, 04:07 AM
HansDouwe Re: VBA code for Permutation... 09-05-2022, 07:28 AM
bsalv Re: VBA code for Permutation... 09-05-2022, 09:27 AM
HansDouwe Re: VBA code for Permutation... 09-05-2022, 10:32 AM
  1. #1
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    VBA code for Permutation and combination

    Hi experts,
    I need a vba code to make all possible combinations of 4 letters from 26 Alphabets (A-Z) in one column.

    for e.g. AAAB, AAAC, BARB, ABCL, etc.

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: VBA code for Permutation and combination

    Post deleted.
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  3. #3
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    Re: VBA code for Permutation and combination

    ??? why it is deleted?

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA code for Permutation and combination

    Hi saravanan2021,

    Using the PERMUTATIONA function that was released with Excel 2013 you are after 456,976 permutations with repetition...

    =PERMUTATIONA(26,4)

    ...verified via =26^4

    So using pgc01's nifty solution from here, this will do the trick for you:

    'Original Thread https://www.excelforum.com/excel-programming-vba-macros/1386523-vba-code-for-permutation-and-combination.html
    'Based on https://www.mrexcel.com/board/threads/all-possible-combinations-with-repeats-allowed.436932
    Option Explicit
     
    Dim arrElements As Variant
    Dim arrResult As Variant
    Dim p As Long
    Sub ListPermWithRepeats()
    
        Dim wsOutput As Worksheet
        Dim lRow As Long
        
        Application.ScreenUpdating = False
        
        Set wsOutput = ThisWorkbook.Sheets("Sheet1") 'Name of sheet for the permutations. Change to suit if needed.
     
        arrElements = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
        p = 4
        ReDim arrResult(1 To (UBound(arrElements) - LBound(arrElements) + 1) ^ p, 1 To 1)
         
        PermutRep 1, "", lRow
        wsOutput.Range("A2").Resize(UBound(arrResult)) = arrResult 'Output from cell A2. Change to suit if needed.
        
        Application.ScreenUpdating = True
    
    End Sub
    Sub PermutRep(ByVal lInd As Long, ByVal sresult As String, ByRef lRow As Long)
        
        Dim i As Long
         
        For i = LBound(arrElements) To UBound(arrElements)
            If lInd = p Then
                lRow = lRow + 1
                arrResult(lRow, 1) = sresult & arrElements(i)
            Else
                PermutRep lInd + 1, sresult & arrElements(i), lRow
            End If
        Next i
        
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  5. #5
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    Re: VBA code for Permutation and combination

    Hi Trebor76,
    your code solved my purpose.

    Thank you so much.

    Similarly Can you give me a solution to create 3 letters word, 5 letter word or 6 letters word.

    I tried changing p = 4 to p = 5 and p = 6.

    But it is not working.

  6. #6
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA code for Permutation and combination

    Permutation is different from combination

  7. #7
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,944

    Re: VBA code for Permutation and combination

    See attached: Sheet2
    Attached Files Attached Files
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  8. #8
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA code for Permutation and combination

    Run this code in a new workbook on sheet1. (Combinations)

    Dim ar, sq, x As Long
    
    Sub jec()
     ar = [transpose(char(row(65:90)))]
     ReDim sq(Application.Combin(26, 4), 0)
     x = 0
     
     Application.ScreenUpdating = False
     
     Gen_comb "", -1, 26 - 4
         
     With Sheets(1).Cells(1, 2)
       .CurrentRegion.ClearContents
       .Resize(x) = sq
     End With
    End Sub
    
    Sub Gen_comb(xStr As String, y As Long, n As Long)
     Dim sp, j As Long
     For j = y + 1 To n
        sp = xStr & ar(j + 1)
        If Len(sp) = 4 Then
          sq(x, 0) = sp
          x = x + 1
        Else
          Gen_comb xStr & ar(j + 1), j, n + 1
       End If
     Next
    End Sub
    Last edited by JEC.; 08-19-2022 at 10:49 AM.

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA code for Permutation and combination

    Permutation is different from combination
    Agreed. John Topley's and your suggested solution provides the 14,950 combinations reconciled using =COMBIN(26,4)

    But based on the initial request...

    Hi experts,
    I need a vba code to make all possible combinations of 4 letters from 26 Alphabets (A-Z) in one column.
    for e.g. AAAB, AAAC, BARB, ABCL, etc.
    ...only ABCL is in the output using these solutions so I think (very possibly wrongly) we need to output all the permutations with repetitions.
    Last edited by Trebor76; 08-19-2022 at 06:52 PM.

  10. #10
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA code for Permutation and combination

    I thought he might want combinations and permuatations.

    VBA code for Permutation and combinations

  11. #11
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    Re: VBA code for Permutation and combination

    Yes I need permutation and combinations.

    Actually it should be more than 14950.

    Can any one share me a code.

  12. #12
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA code for Permutation and combination

    Yes good call

  13. #13
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,944

    Re: VBA code for Permutation and combination

    For permutation, change "C" to "P" in A1 !!!

  14. #14
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,659

    Re: VBA code for Permutation and combination

    This for combination of 2 or 3 (change the "c" value in code)
    With more combination lettes, add more "for..loop": l,m,n (6 combination), Note that with 6 combination of 26, it could be 381 milion cases and could not paste into a single column.
    PHP Code: 
    Option Explicit
    Sub test
    ()
    Dim i&, j&, k&, x&, c&, arr()
    ' could be 2 or 3
    ReDim arr(1 To 26 ^ c, 1 To 1)
    For i = 65 To 90
        For j = 65 To 90
            If c = 2 Then
                x = x + 1
                arr(x, 1) = Chr(i) & Chr(j)
                If x = (26 ^ c) Then GoTo Out
                GoTo Outj
            End If
                For k = 65 To 90
                        x = x + 1
                        arr(x, 1) = Chr(i) & Chr(j) & Chr(k)
                Next
    Outj:
        Next
    Next
    Out:
    Range("A:A").ClearContents
    Range("A1").Resize(UBound(arr), 1).Value = arr
    End Sub 
    Last edited by bebo021999; 08-26-2022 at 12:49 AM.
    Quang PT

  15. #15
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA code for Permutation and combination

    14950 is the correct amount of combinations. I think you mean permutations

  16. #16
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    Re: VBA code for Permutation and combination

    Yes. I need permutations too

  17. #17
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: VBA code for Permutation and combination

    I think both post 4 and 6 give you permutations

  18. #18
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    words of 5 characters out of 26 characters
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  19. #19
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    Re: VBA code for Permutation and combination

    Oh Man,

    Awesome!!!!!!!!!!!!

    Can I change iNumbers = 5 to iNumbers = 7 to make 7 letter word and so on?
    Last edited by saravanan2021; 08-30-2022 at 05:19 AM.

  20. #20
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    NO you 'll have memory problems while executing the macro because the result for 6 letters is 1.000.000 rows by 166 columns.
    I can rewrite the macro, so that you can handle blocks of, for example, 1.000.000 rows by 10 columns, so in 16 steps 'll have all those possibilities.

    What 'll be the benefits of having those immense data ?

    7 letters = 1.000.000 rows * 3.316 columns
    8 letters = almost 4 complete worksheets
    Last edited by bsalv; 08-30-2022 at 05:43 AM.

  21. #21
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VBA code for Permutation and combination

    Quote Originally Posted by bsalv View Post
    the result for 6 letters is 1.000.000 rows by 166 columns.
    7 letters = 1.000.000 rows * 3.316 columns
    8 letters = almost 4 complete worksheets
    It's much more. You look at the number of permutations (no doubles allowed), but if you look at the examples given then it's about the number of possible strings (doubles allowed)

  22. #22
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    Re: VBA code for Permutation and combination

    oH OK.

    CAN YOU GIVE FOR ATLEASET 6 LETTERS WORDS?

  23. #23
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    i didn't test it and it 'll take a while ...
    Attached Files Attached Files

  24. #24
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VBA code for Permutation and combination

    Solution with a formula

    Please try in A1 and copy down 456,976 times:
    Formula: copy to clipboard
    =CHAR(MOD((ROW()-1)/26^3,26)+65)&CHAR(MOD((ROW()-1)/26^2,26)+65)&CHAR(MOD((ROW()-1)/26,26)+65)&CHAR(MOD(ROW()-1,26)+65)
    An other possibilty for users Excel 365 and 2021:

    Please try in C1 (or in any other cell you like) :
    Formula: copy to clipboard
    =LET(c,SEQUENCE(26^4,,0),CHAR(MOD((c)/26^3,26)+65)&CHAR(MOD((c)/26^2,26)+65)&CHAR(MOD((c)/26,26)+65)&CHAR(MOD((c),26)+65))
    Last edited by HansDouwe; 09-03-2022 at 02:46 PM.

  25. #25
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    @Hans, the question was 6 letters = 26^6 = almost 309 000 000.
    that 'll be a problem, i suppose ...

  26. #26
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VBA code for Permutation and combination

    My answer is based on the question in #1. Based on the examples in #1, all possible sequences of 4 letters are meant. So 26 to the power of 4 possibilities.

    Furthermore, I see that there is a whole discussion going on about which options are intended exactly.
    If not, could you please modify the examples in post #1 and make it as clear as possible whether you want combinations, or permutations.
    Otherwise I have to go through all 23 previous posts carefully to figure out what is meant and that gets very confusing.

    Example "ABCD" is 1 combination, and with these letters no more combinations can be made, but 24 permutations can be made. You must also indicate whether or not a letter may appear more than once in 1 word of 4 letters.

  27. #27
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    The last question was all 6-letter-words that can be made with the alphabet = 26^6.
    Your answer #23 was 26^4, so it's almost the same but 2 gears higher ...
    the only question, what 's the benefit of that ???

  28. #28
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA code for Permutation and combination

    Put AAAA in A1 and then put
    =IF(RIGHT(B1,3)="zzz", IF(MID(B1,1,1)="Z","A",CHAR(CODE(MID(B1,1,1))+1)), LEFT(B1,1)) & IF(RIGHT(B1,2)="ZZ", IF(MID(B1,2,1)="Z","A",CHAR(CODE(MID(B1,2,1))+1)), MID(B1,2,1)) & IF(RIGHT(B1,1)="Z", IF(MID(B1,3,1)="Z","A",CHAR(CODE(MID(B1,3,1))+1)), MID(B1,3,1)) & IF(RIGHT(B1,1)="Z","A",CHAR(CODE(RIGHT(B1,1))+1))

    in B1 and drag down
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  29. #29
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VBA code for Permutation and combination

    I also wonder if we misunderstood the OP. The benefit of such a list seems to me to be comparable to the benefit of a list with all numbers from 1 to 1,000,000,000.
    Without looking, you know what it says and how often a number starts with 1 or a string start with an A for example.
    .
    If you look at 6 letters, there are about 309 million different 6 letter strings, but 'only' 230 thousand combinations.

    @Saravanan, Did I understand you correctly, do you want a list of all 309,000,000 different possible 6-letter strings?

  30. #30
    Registered User
    Join Date
    05-14-2021
    Location
    India
    MS-Off Ver
    Office 10
    Posts
    23

    Re: VBA code for Permutation and combination

    @Saravanan, Did I understand you correctly, do you want a list of all 309,000,000 different possible 6-letter strings?

    Yes you are right.

  31. #31
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    this are the numbers :, i used the wrong numbers in 20.
    6 is 309 columns of 1 000 000 cells
    Attached Images Attached Images
    Last edited by bsalv; 09-03-2022 at 05:15 PM.

  32. #32
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VBA code for Permutation and combination

    Those are the correct numbers indeed. And with with 6 letters you already arrive at a required storage for de values only of about 2 GB (300 million * 6).
    This is already the theoretical maximum of a 32-bit excel, but it crashes far before.
    Even for a 64-bit Excel, such a file will already be full for many systems because of the memory limitations of the device on which Excel runs.

  33. #33
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    "...but it crashes far before."
    At least you tried, i wrote the macro and stopped it because it took too long ..

  34. #34
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VBA code for Permutation and combination

    OK.
    Apart from the question of why, an adequate solution seems to me to be VBA-solution that writes the data to an external file.
    Reserve 2.5 GB diskspace for 6 letters, 60 GB for 7 letters, 2 TB for 8 letters.

    I'm not a VBA-expert.
    Perhaps a VBA expert can help you with this.

  35. #35
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,449

    Re: VBA code for Permutation and combination

    just for the fun: 6 letters = 309 million permutations = 2,11 GB filesize (as predicted by @HansDouwe)in almost 10 minutes
    Attachment 794892
    Attached Files Attached Files
    Last edited by bsalv; 09-05-2022 at 09:33 AM.

  36. #36
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,466

    Re: VBA code for Permutation and combination

    If this answers your question, consider adding reputation (by clicking Add reputation) at the bottom left of the answer to all the helpers you think deserve it.

    Have fun with the list.
    It makes very clear what is possible with only 6 letters.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Permutation & Combination
    By Jiaying in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 12-03-2015, 04:55 AM
  2. Permutation Combination
    By nilesh.parekh in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-05-2013, 09:24 AM
  3. Permutation and Combination
    By DEWAL in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-08-2013, 08:31 AM
  4. Combination and permutation
    By stanlori in forum Excel General
    Replies: 0
    Last Post: 05-15-2011, 09:10 PM
  5. Permutation Combination
    By Amarjeet Singh in forum Excel General
    Replies: 3
    Last Post: 03-18-2011, 04:43 AM
  6. 5 Digit Combination/Permutation??
    By TJD in forum Excel General
    Replies: 4
    Last Post: 07-07-2008, 01:07 PM
  7. Permutation and combination
    By vishu in forum Excel General
    Replies: 2
    Last Post: 05-19-2005, 03:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1