+ Reply to Thread
Results 1 to 11 of 11

Need to compare 2 columns and align them next to the match

  1. #1
    Registered User
    Join Date
    05-07-2013
    Location
    austin texas
    MS-Off Ver
    Excel 2007
    Posts
    5

    Need to compare 2 columns and align them next to the match

    Using Excel 2007. I need a Macro script that will take my columns B-J and match the value of B to the value of A and move the entire row with B to match A.


    Example Before
    A B C D ...
    1
    2 7 ox green
    3
    4 3 cat brn
    5 1 dog red
    6 4 bird blue
    7 5 rat black


    Example After
    A B C D ...
    1 1 dog red
    2
    3 3 cat brn
    4 4 bird blue
    5 5 rat black
    6
    7 7 ox green

    Thank You in advance!!!
    Last edited by naenightlucky213; 05-07-2013 at 07:46 PM. Reason: Title

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Help!! Simple Script

    Hi, naenightlucky213,

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    05-07-2013
    Location
    austin texas
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Help!! Simple Script

    Done
    Thank you

  4. #4
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Need to compare 2 columns and align them next to the match

    This code retains your existing formats, not sure if you wanted this, also works if numbers in ColA are not consecutive, not sure if you wanted this either.
    Code is simpler if these 2 conditions are not required.
    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Need to compare 2 columns and align them next to the match

    on second thoughts ...
    scripting dictionary is quite fun, but doesn't (didn't) work on a Mac, and not everyone likes it.
    you could instead try
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-07-2013
    Location
    austin texas
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Need to compare 2 columns and align them next to the match

    Hello,
    That almost worked, but it moved a lot of the loans that did have a match all the way to the bottom below everything else. I need them all to match up. Columns B-T need to move together and match up to B's match on A's column. They are numbers in both column A & B. Some will be in one and not the other.

  7. #7
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Need to compare 2 columns and align them next to the match

    Quote Originally Posted by naenightlucky213 View Post
    Hello,
    That almost worked, but it moved a lot of the loans that did have a match all the way to the bottom below everything else. I need them all to match up. Columns B-T need to move together and match up to B's match on A's column. They are numbers in both column A & B. Some will be in one and not the other.
    Both of those codes did just as you requested with the data you gave.

    If you're using different data, whose layout I can't guess, then perhaps you could post a representative sample.

  8. #8
    Registered User
    Join Date
    05-07-2013
    Location
    austin texas
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Need to compare 2 columns and align them next to the match

    Hello,
    I am not sure why it did not work. But it moved a lot of the invoice numbers. And it also created multiple tabs. I need all of these changes to take place on the same sheet. I will attach column A & B to show you what it did but I will not include the other columns.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    05-07-2013
    Location
    austin texas
    MS-Off Ver
    Excel 2007
    Posts
    5

    Smile Re: Need to compare 2 columns and align them next to the match

    Hello,
    I really do not understand where the issue with the code is. But it works for some of them but then there is a bundle of them that dont get matched up with their match in Column A. Please see the attachement & scroll all the way to the last row with info in it to see what happened. Those have matches but arent aligned with them.
    Thank you again.
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Need to compare 2 columns and align them next to the match

    Quote Originally Posted by naenightlucky213 View Post
    Hello,
    I really do not understand where the issue with the code is. But it works for some of them but then there is a bundle of them that dont get matched up with their match in Column A. Please see the attachement & scroll all the way to the last row with info in it to see what happened. Those have matches but arent aligned with them.
    Thank you again.
    OK. Different data setup does make a difference.
    Is it something like this?
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    11-09-2009
    Location
    london, england
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Need to compare 2 columns and align them next to the match

    Wonder if the last piece of code by Kalak has worked as I was working on a similar issue and found that I used a different approach.
    At a high level the process is
    - create a temporary worksheet to hold the results
    - using the data worksheet use vlookup iteratively on each row to match A in B
    - if a match exists then iteratively use vlookup across the remaining columns
    - put he results from the vlookup into the temporary worksheet
    - rename the temporary worksheet

    I chose this approach for my piece of work because I had such a headache with shifting rows up and down, accidentally overwriting data or not clearing cells.
    Below is the attached code and modified to work with your sample dataset that you provided with your first post.
    I'm not sure if this is the most efficient method in terms of speed with regards to dictionary vs vlookup, but I guess is depends the size of your dataset.

    If you have already found another solution then that is great.

    Please Login or Register  to view this content.

+ 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