+ Reply to Thread
Results 1 to 16 of 16

VBA for all possible combinations without repetition

  1. #1
    Registered User
    Join Date
    01-25-2021
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    VBA for all possible combinations without repetition

    Hey Guys,

    I'm new to this forum. My name is Hugues. I need your help for a VBA code. My excel sheet contains only 2 columns called: List (cell A1) and results (cell C1).

    In the list columns(A2, A3, A4 and so on), i have a list of words. What i want the VBA to accomplish is to combine(permute without repetition of the same words in the sentence) all the words from the list up to a maximum of a combinations of 3 words. Do you think it would be possible?

    Many thanks in advance for your help !
    Attached Files Attached Files
    Last edited by McGyver270; 01-25-2021 at 06:17 PM. Reason: Sheet added

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,890

    Re: VBA for all possible combinations without repetition

    read the yellow banner at the top of this page
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    01-25-2021
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Re: VBA for all possible combinations without repetition

    Sorry Alan.

    I've added the sheet to my original post.

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

    Re: VBA for all possible combinations without repetition

    Hi McGyver270,

    Welcome to the forum!!

    Change any of the four variables I've commented with "Change to suit if necessary" and then try this while on the sheet that has the data in the 'strMyCol':

    Please Login or Register  to view this content.
    To check the number of combinations use the COMBIN function i.e. =COMBIN(6,3) in your example.

    Keep in mind that permutations can grow quickly - so much so that they can exceed the number of rows on an Excel sheet!!

    Note the credit in the code to Weaver for the User Defined Function (UDF) that does the grunt work.

    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
    01-25-2021
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Re: VBA for all possible combinations without repetition

    Hi Robert,

    Thank you very much for your code, it's greatly appreciated.

    I'm not sure if i understand your comment Change any of the four variables I've commented with "Change to suit if necessary" and then try this while on the sheet that has the data in the 'strMyCol':

    Can you expand more please?

    Thank a lot.

    Hugues

    Edit: While running the macro, i get the following error: ''compile error user-defined type not defined''. Any idea what is causing this? Thank again for your time.
    Last edited by McGyver270; 01-26-2021 at 01:01 AM. Reason: Comment added

  6. #6
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: VBA for all possible combinations without repetition

    You could do this with formulas.

    E1:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    E2:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    F2:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    G2:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    H2:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Select E2:H2 and fill down until formulas return #N/A errors.

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

    Re: VBA for all possible combinations without repetition

    I'm not sure if i understand your comment Change any of the four variables I've commented with "Change to suit if necessary":
    You can change the values of these four variables in the ListAllCombinations macro to meet your needs, if required:

    Please Login or Register  to view this content.
    try this while on the sheet that has the data in the 'strMyCol':
    So you need to run the ListAllCombinations macro while you're on the Feuil1 tab and the data needs to be in the column that's been assigned to the "strMyCol" variable (A in my original code).

    Edit: While running the macro, i get the following error: ''compile error user-defined type not defined''. Any idea what is causing this?
    Not sure why but there's a recordset variable that is in the function. I have removed it from the following and it then ran without issue (you could make a reference to Microsoft ActiveX Data Objects in the Visual Basic Editor to also solve the issue but it seems to serve no purpose (that I can see anyway)):

    Please Login or Register  to view this content.
    Last edited by Trebor76; 01-26-2021 at 02:26 AM.

  8. #8
    Registered User
    Join Date
    01-25-2021
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Re: VBA for all possible combinations without repetition

    Robert,

    Many thanks for taking the time to make the correction, it work fine except that its not the results i was looking for. It doesn't give me all the combinations possible. I will give you another example of possible outcome i'm looking for:

    Lets says we have the words: Cat, Dog, Monkey and Bird

    The functions i need should return the combinations:

    Cat
    Dog
    Monkey
    Bird
    Cat-dog
    Cat-Monkey
    Cat-bird
    Dog-monkey
    Dog-bird
    Dog-cat
    Monkey-bird
    Monkey-cat
    Monkey-dog
    Bird-cat
    Bird-dog
    Bird-monkey
    Cat-dog-monkey
    Cat-dog-bird
    Cat-monkey-bird
    Cat-monkey-dog
    Cat-bird-dog
    Cat-bird-monkey
    Dog-cat-bird
    Dog-cat-monkey
    Dog-bird-monkey
    Dog-bird-cat
    Dog-monkey-bird
    Dog-monkey-cat
    Bird-dog-cat
    Bird-dog-monkey
    Bird-cat-dog
    Bird-cat-monkey
    Bird-monkey-dog
    Bird-monkey-cat
    Monkey-dog-cat
    Monkey-dog-bird
    Monkey-cat-dog
    Monkey-cat-bird
    Monkey-bird-cat
    Monkey-bird-dog

    The only condition is that the formula should not give a combination with the same word, ie: Cat-cat or in the case of 3 words: cat-dog-cat

    Sorry again, i should have explained it better the first time.

    Hugues

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

    Re: VBA for all possible combinations without repetition

    There has to be a more efficient way but this works:

    Please Login or Register  to view this content.

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

    Re: VBA for all possible combinations without repetition

    Here's a more succinct version that can still be improved:

    Please Login or Register  to view this content.

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

    Re: VBA for all possible combinations without repetition

    Last try:

    Please Login or Register  to view this content.

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA for all possible combinations without repetition

    Another Option...
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  13. #13
    Registered User
    Join Date
    01-25-2021
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Re: VBA for all possible combinations without repetition

    Robert,

    Thank you very much for your work. Macro2 work better for my case. If i can make a contribution for your work, i will be more than happy to send you some $

    A thank also to hrlngrv and sintek for your contribution to this thread.

    Hugues

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

    Re: VBA for all possible combinations without repetition

    Thank you very much for your work. Macro2 work better for my case.
    You're welcome. Thanks for letting us know.

    If i can make a contribution for your work, i will be more than happy to send you some $
    No, this section of the forum is free so I will not accept payment. If you could mark the thread as solved it would be appreciated.

    Regards,

    Robert

  15. #15
    Registered User
    Join Date
    01-25-2021
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    6

    Re: VBA for all possible combinations without repetition

    No problem. Thank much again, Robert !

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

    Re: VBA for all possible combinations without repetition

    Thank you (and for the rep)

+ 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. [SOLVED] Combinations without repetition - VBA code partially working
    By Arthfael in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-14-2015, 05:48 AM
  2. [SOLVED] Sum without repetition
    By ratkiller in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-19-2013, 09:20 AM
  3. Finding possible combinations & listing the wanted combinations
    By Zoke in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 07-16-2012, 03:41 PM
  4. [SOLVED] Repetition
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-29-2012, 06:55 PM
  5. Sampling Without Repetition
    By Provoxt in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-13-2010, 10:23 AM
  6. Repetition
    By EAMOG in forum Excel General
    Replies: 5
    Last Post: 12-15-2008, 06:37 PM
  7. Repetition
    By filmor in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-06-2008, 02:55 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