+ Reply to Thread
Results 1 to 10 of 10

Random Pairs Macro

  1. #1
    Registered User
    Join Date
    04-08-2013
    Location
    Argentina
    MS-Off Ver
    Excel 2007
    Posts
    4

    Random Pairs Macro

    Given a list of n names, I would like to create random pairs, with no repetition (no pairs with the same person), just like a secret santa gift selection, or a round robin tournament with only one round. The idea is to create a spreadsheet to program audits between personnel of a company. So "Name n" should audit "Name i", and there's no problem if also that same "Name i" audits that same "Name n", but no "Name n" could audit himself. First attempt was to list the names in a column, create a random number in the next column, then rank the random column, and match the ranked number to a name using vlookup to create the pair. This is an example for 5 names... but it should work for any number of given names.

    # name random ranking vloockup
    1 name1 0,79178235 3 name3
    2 name2 0,399734465 5 name5
    3 name3 0,81276639 1 name1
    4 name4 0,803179442 2 name2
    5 name5 0,583473851 4 name4

    It works pretty good, but every time the ranked number is the same than the list number (#), the pair results in the same person (the same thing happens if random numbers are repeated). So I made an other column to spot this error (comparing both names of the pair), and recalculate the spreadsheet until the error disappears, using f9 key every time (which would create new random numbers).

    # name random ranking vloockup REPEAT!
    1 name1 0,443535597 2 name2
    2 name2 0,891630138 1 name1
    3 name3 0,216194185 5 name5
    4 name4 0,24676049 4 name4 ERROR
    5 name5 0,301665665 3 name3

    The longer the list, the faster I get to a good result (less chances for a ranked random number to coincide with the list number), but still want to create a spreadsheet that works without manual iterations.

    I'm not very familiar with macros, but is there a way to create a macro that reads the value of one specific cell, say the header of the error column which could count in the column if there's any repetition in any pair and give a status (OK, REPEAT), and then recalculate the spreadsheet, until errors disappear? Then by clicking a button with this macro, random numbers should be calculated and recalculated until pairs are matched without repetition, end of the problem! Wish it was that easy!!!

    (Or may be there's better way to aboard this problem)
    Thanks!!!

  2. #2
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,843

    Re: Random Pairs Macro

    This solution works but I would be surprised if no one found a better way of doing it.

    To test it I needed a list of random names. What do you know, everything is available on internet...
    http://listofrandomnames.com/
    Attached Files Attached Files
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  3. #3
    Registered User
    Join Date
    04-08-2013
    Location
    Argentina
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Random Pairs Macro

    Jacc, thanks for your reply! Didnīt know about that index formula!
    The solution looks pretty good, but I'm afraid I canīt apply it in this case, as every member of the list needs to be audited, but also must audit someone. I think I did'n make this point clear in the first message, or didn't thought about it:
    1st condition: "Name n" must be paired with "Name i" (ie "Thom" audits "John")
    2nd condition: "Name j" (or "Name i") must be pared with "Name n" ("Mike" or "John" audit "Thom")
    Splitting the list in two doesn't allow this last condition. In other words, a name must be in both columns that make a pair, but the name mustn't be in the same line.
    Many thanks

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Random Pairs Macro

    Hi Sorry I am missing something but.

    Why not sort your list of names randomly. create a random number in a helper column and sort by that column.

    Then paste your two sets of your list of names next to each other but offset by one row.

    Move the last name to the top, opposite the first name.

    Done.

  5. #5
    Registered User
    Join Date
    04-08-2013
    Location
    Argentina
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Random Pairs Macro

    It could work also, but it must be automated as I donīt know who is the person that is going to manage this pair generator spreadsheet yet (what if that person is not a friend of excel). I would rather do it as simple as possible, this means, create the name list, hit a button and voila, pairs are ready... I thought about a way of doing it, but I donīt know how to create a loop with a macro, what about creating a macro with this last procedure? (I donīt know how to do it either)

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Random Pairs Macro

    Ok I will create the macro for you

    If I put the two lists in columns A and B would that do for you?

    I cannot see that any loops would be required for this.

    It is a max of six lines of code.
    Last edited by mehmetcik; 04-09-2013 at 03:05 PM.

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Random Pairs Macro

    Ok

    your code is as follows:-

    Please Login or Register  to view this content.

    A sample sheet is attached.

    Enjoy.
    Attached Files Attached Files

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Random Pairs Macro

    There's a workbook with two ways to do this at https://www.box.com/s/ug8etrfi404n3au5md5g
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Registered User
    Join Date
    04-08-2013
    Location
    Argentina
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Random Pairs Macro

    Great job! That will work just fine, thank you!!!

  10. #10
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,843

    Re: Random Pairs Macro

    Another solution
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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