+ Reply to Thread
Results 1 to 14 of 14

trouble with matching values in two columns with a twist

  1. #1
    Registered User
    Join Date
    01-31-2015
    Location
    perth, australia
    MS-Off Ver
    2010
    Posts
    16

    trouble with matching values in two columns with a twist

    Hi All,
    I basically need help with the following staffing issue in connection with our Annual leave “ROSTERS”. we don't have a system at work presently to deal with annual leave swaps.

    At work all Staff are assigned annual Leave 6wks per year, these 6 week blocks are represented by alpha letters such as A,B, D, E, F, etc. As I said these letters correspond to 6 week blocks within each calendar year, this allocated leave letter remains with you throughout your employment. So for obvious reasons this doesn’t suit every member of staff, so our HR Dep’t kindly allows us to swap with other willing staff members, so basically if we could find another staff member willing to swap letters then they will allow us to do the swap, on a one time only basis per year, meaning once swapped then you cannot later on then change your mind and request to swap the swap. How staff achieve this presently is to send out an e-mail to all staff requesting what you Have and what period you Want (it's a fairly large company with over 300 staff).

    This solution works reasonably well, but is obviously flawed (I'll explain why later on) as some staff will lose out. As you will only get a response(via Email) if someone has what you want e.g meaning Staff member1 has "A" and wants "B" period and staff member20 has "B" and really wants "A", because then its just a matter of both parties getting together and filling in a request form too give to the HR Dept(2Xway swap).

    What I would like to do is create a register so as our HR Dep’t can accommodate all our staff a little more better, then some staff will not lose out. Following is a very basic example of why staff do lose out on swaps that actually are available but get missed due to swaps only being available via e-mail (2Xway swap).

    E.g. take the following scenario:

    If a staff member1 sends an email saying that he has “A” and wants “C”, and another staff member2 reads this request who does have “C” but he wants “B”, then he's not going to answer the e-mail and so both staff members will lose out! as the person with “C” is not interested in taking “A” as he wants “B” which is a whole different six week block within the that year (it could be for a wedding or something so specific dates are important to get). But if we introduce a third staff member25 who say has “B” and wants “A”, then each of these 3Xstaff members would then be able to swap with each other as all staff involved would get what letter they wanted. (By the way I forgot to mention if staff member25 read any of the two previous e-mails he would not have answered either of them, I hope that makes sense
    A - C
    C - B
    B - A

    So the solution would be to create a register and then somehow be able to run some sort of formula to find the matches/swaps even it were a 4,5 or 6Xway swap as they all would be happy.
    Here’s what I have so far in VBA but it does not take into account if all parties involved are satisfied. As presently it just finds matches and swaps whether or not the swap has been satisfied at it's end, so I think it needs nested loops holding variables for each swap so it can undo if after swapping out if all parties involved in the swap are happy, but this is something way beyond my vba skills. Any help with this is appreciated. thanks(see attached xls)

    PHP Code: 

    Sub SWAPpart1
    ()
    Dim lRow As Longx1 As LongvarTmp As VarianttmpArr() As String

    lRow 
    Range("A" Rows.Count).End(xlUp).Row


    With Range
    ("C2:C" lRow)
    .
    Formula Replace("=IFERROR(""A""&AGGREGATE(15,6,ROW($B$2:$B$@)/($A$2:$A$@=B2),COUNTIF(B$2:B2,B2)) & ""-A""&ROW(),"""")""@"lRow)
    .
    Value = .Value
    End With
    End Sub 
    PHP Code: 

    Sub SWAPRun
    ()
    Dim lRow As Longx1 As LongvarTmp As VarianttmpArr() As String

    lRow 
    Range("A" Rows.Count).End(xlUp).Row


    With Range
    ("C2:C" lRow)
    .
    Formula Replace("=IFERROR(""A""&AGGREGATE(15,6,ROW($B$2:$B$@)/($A$2:$A$@=B2),COUNTIF(B$2:B2,B2)) & ""-A""&ROW(),"""")""@"lRow)
    .
    Value = .Value
    End With

    With Range
    ("D2:D" lRow)
    .
    Formula Replace("=IFERROR(""A""&AGGREGATE(15,6,ROW($B$2:$B$@)/($A$2:$A$@=B2),COUNTIF(B$2:B2,B2)) & ""-A""&ROW(),"""")""@"lRow)
    .
    Value = .Value
    End With


    For x1 2 To lRow
    If Range("C" x1).Value vbNullString Then
    ' NOTHING
    Else
    tmpArr = Split(Range("C" & x1).Value, "-")
    varTmp = Range(tmpArr(0)).Value
    Range(tmpArr(0)).Value = Range(tmpArr(1)).Value
    Range(tmpArr(1)).Value = varTmp
    End If
    Call SWAPpart1
    Next x1
    End Sub 
    Attached Files Attached Files

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: trouble with matching values in two columns with a twist

    Hi Gint,

    Welcome to the Forum!

    It's a start:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: trouble with matching values in two columns with a twist

    This is how I would do that:

    A: Names
    B: Current Vacation
    C: Wanted Vacation

    Macro:
    Please Login or Register  to view this content.
    Fun little puzzle.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    01-31-2015
    Location
    perth, australia
    MS-Off Ver
    2010
    Posts
    16

    Re: trouble with matching values in two columns with a twist

    previous notes: - Here’s what I have so far in VBA but it does not take into account if all parties involved are satisfied. As presently it just finds matches and swaps whether or not the swap has been satisfied at its end
    Thanks for your input, So sorry but the solution is a lot more complex than it might first seems, as I said earlier it'll need some sort of multi nested loops to handle the swaps as the wanted's cannot swap unless the person they swap with is also satisfied, so for the small sample.xls that I have attached only four staff could swap with that simple scenario.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-31-2015
    Location
    perth, australia
    MS-Off Ver
    2010
    Posts
    16

    Re: trouble with matching values in two columns with a twist

    Quote Originally Posted by JBeaucaire View Post
    This is how I would do that:

    A: Names
    B: Current Vacation
    C: Wanted Vacation

    Fun little puzzle.
    Here’s what I have so far in VBA but it does not take into account if all parties involved are satisfied. As presently it just finds matches and swaps whether or not the swap has been satisfied at its end
    Thanks also for your input, but again the solution is a lot more complex than it might first seems to you, as I said earlier I think it'll need some sort of multi nested loops to handle the swaps as the wanted's cannot swap unless the person they swap with is also satisfied which has a domino affect on the next swaps etc etc, so when a match is found it also needs to check that the swap its swapping with is also satified and if it doesn't then it retracts the swap, so for the small simple simplesample.xls that I have attached only four staff could swap with that scenario.
    Last edited by gint32; 02-01-2015 at 02:57 AM.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: trouble with matching values in two columns with a twist

    My macro seems to manage a solution for up to 10 domino swaps, it works well on the sample data. Rather than saying "I don't like this solution" explain why/how you reached that conclusion and provide the new sample data you used to reach that conclusion. Else I'll stand by the offered macro.

    It should be simple for you to paste in other sets of sample data in A:C and run the macro, review the results.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: trouble with matching values in two columns with a twist

    Hi Gint,

    Thanks for the rep!

    Well they didn't want what they had so to swap one unwanted for another unwanted isn't really unwonted!

    Data Range
    A
    B
    C
    D
    E
    F
    G
    H
    1
    Has
    Wants
    2
    A
    D
    D
    6
    3
    B
    A
    D
    5
    A
    6
    4
    C
    A
    E
    7
    5
    D
    B
    B
    3
    6
    D
    C
    A
    2
    D
    3
    7
    E
    C
    C
    4

    Please Login or Register  to view this content.
    Last edited by xladept; 02-01-2015 at 06:42 PM.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: trouble with matching values in two columns with a twist

    I've been trying to update my last post with this new code:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-31-2015
    Location
    perth, australia
    MS-Off Ver
    2010
    Posts
    16

    Thumbs up Re: trouble with matching values in two columns with a twist

    Quote Originally Posted by JBeaucaire View Post
    My macro seems to manage a solution for up to 10 domino swaps, it works well on the sample data. Rather than saying "I don't like this solution" explain why/how you reached that conclusion and provide the new sample data you used to reach that conclusion. Else I'll stand by the offered macro.

    It should be simple for you to paste in other sets of sample data in A:C and run the macro, review the results.
    So sorry, but I have posted the why/how your code doesn't work..but here it is again.
    Also, I never said I didn't like your code and I am very greatful for your input regardless of whether it works or not I was only pointing out that it didn't work as you might of thought. take the following simple example data this doesn't work with your code.

    Name Has Wants Swaps With
    Name 1 A D Name 2
    Name 2 B C Name 3
    Name 3 C A Name 1
    Name 4 D F Name 1

    Nobody should be able to swap Yet when running your code is shows that they can swap! But in actuality nobody can, because if the swaps take place as your code suggests, then some people will end up with letters(holiday dates)at a time when they don't want to take a vacation. So all people involved in any swapping need to be happy(meaning satified) before swapping(e.g letting go of the letter they have been allocated, as they can't swap a swap after the fact). Hence...
    Thanks also for your input, but again the solution is a lot more complex than it might first seems to you, as I said earlier I think it'll need some sort of multi nested loops to handle the swaps as the wanted's cannot swap unless the person they swap with is also satisfied which has a domino affect on the next swaps etc etc, so when a match is found it also needs to check that the swap its swapping with is also satified and if it doesn't then it retracts the swap, so for the small simple simplesample.xls that I have attached only four staff could swap with that scenario.
    Much appreciated. see attached
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    01-31-2015
    Location
    perth, australia
    MS-Off Ver
    2010
    Posts
    16

    Re: trouble with matching values in two columns with a twist

    Quote Originally Posted by xladept View Post
    I've been trying to update my last post with this new code:

    Please Login or Register  to view this content.
    Sorry, but your sample output is way beyond my understanding. I cannot figure whats happening with it. Can you explain(simply) from your output "Who swap with who?" please!
    and many thanks for remaining interested throughout.

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: trouble with matching values in two columns with a twist

    I see what happened. Try this new version.
    Please Login or Register  to view this content.

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: trouble with matching values in two columns with a twist

    Data Range
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    O
    P
    1
    Has
    Wants
    2
    A
    D
    D
    6
    Wants D and got it by swapping with line 6
    3
    B
    A
    D
    5
    A
    6
    Wants A and got it by swapping with line 5 and then line 6
    4
    C
    A
    E
    7
    Wants A out of luck got stuck with E by swapping with line 7
    5
    D
    B
    B
    3
    Wants B and got it by swapping with line 3
    6
    D
    C
    A
    2
    D
    3
    Wants C out of luck still has D but swapped with line 2 and then line 3
    7
    E
    C
    C
    4
    Wants C and got it by swapping with line 4
    8
    After these swaps we've made four of the six happy one of the dissatified has the same unwanted and the other has a different unwanted

  13. #13
    Registered User
    Join Date
    01-31-2015
    Location
    perth, australia
    MS-Off Ver
    2010
    Posts
    16

    Re: trouble with matching values in two columns with a twist

    Quote Originally Posted by JBeaucaire View Post
    I see what happened. Try this new version.
    Please Login or Register  to view this content.
    thanks, I gave the new version a try, the only output I get with it is a replica(exact match) of what ever the values are in column 1. Unless I am missing something. Many thanks though for attempting to try and solve this puzzle.

  14. #14
    Registered User
    Join Date
    01-31-2015
    Location
    perth, australia
    MS-Off Ver
    2010
    Posts
    16

    Re: trouble with matching values in two columns with a twist

    My apologies for my earlier comment as for some reason unknown to me it did not work the first time around and so many thanks for your VBA as it does almost work, but it does still miss some of the matches, though I am not quite sure why, but I recorded a macro on how I sorted the data after running your vba code, so as you could see if there was a possible way forward/solution with this. If you get the time could you step through the macro to see what and how I have done this. Its a statement of the obvious I know “that my recorded macro will not work with other data”, but hopefully it will give a way forward in what I think would be the best way to solve this. First thing is if it’s in(the letter) one column and not the other, then it has to be removed from the equation to a temporary dud-area (till finished) and then returned ready for any new future entries, then begin searching for matches, After hundreds of sorting columns manually I now think two way swaps should be the last type of the search to happen, as I’ll show why. Take the following test data. Say we had matched the two first 2Xway swap using AC&CA. Now with the C&A pair used then the corresponding 21X way swap would not have been able to take place. Also, I have noticed that (after pairing the way I have done similar to below) then if you match any letter in Col A with any letter after that in Col B then all involved can safely swap as is the same the other way round ColB with anything matching in ColA form that point forward. Anyhows as you can see from the below sample most of the letters can swap in various places throughout, in essence there are many many possible swaps within this solution, but none of which would use all of these letters. So only the matches that contain the most number of swaps is in my opinion the best solution. I.e. the more people/leave-letters involved then the more people are satisfied and are happy.

    A < > C …….. A <>B
    A < > B …….. B< > E
    A< > D …….. E< > C
    A< > D …….. C< > D
    A< > G …….. D< > E
    B< > E …….. E< > H
    B< > A …….. H< > F
    C< > D …….. F< > G
    C< > A …….. G< > H
    C< > E …….. H< > C
    D< > E …….. C< > A
    D< > C …….. A< > D
    D< > B …….. D< > C
    E< > C …….. C< > E
    E< > H …….. E< > A
    E< > A …….. A< > D
    F< > G …….. D< > B
    F< > A …….. B< > A
    G< > H …….. A< > G
    G< > F …….. G< > F
    H< > F …….. F< > A
    H< > C ……..
    Attached Files Attached Files
    Last edited by gint32; 02-20-2015 at 06:02 PM.

+ 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] Matching values by two same columns
    By breezyguy in forum Excel General
    Replies: 5
    Last Post: 11-07-2014, 11:31 PM
  2. Replies: 6
    Last Post: 04-11-2014, 04:45 AM
  3. matching values based on criteria and return values from another columns
    By lizard54 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2012, 11:29 AM
  4. [SOLVED] Matching when values are in several possible columns
    By mgreg in forum Excel General
    Replies: 6
    Last Post: 06-13-2012, 11:10 AM
  5. Matching Values from different columns
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-13-2006, 02:28 PM

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