+ Reply to Thread
Results 1 to 24 of 24

VBA - Code to compare 2 columns

  1. #1
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    VBA - Code to compare 2 columns

    Hi,

    The code below was provided by another poster at Ozgrid. As of now, it will only compare column B to column A and return the differences in column C. I may need to use the code to compare column C with column A and B and return the differences in Column D, etc, but I don't understand any of it.

    Can someone break it down and add some explanation text to help me understand the code?

    Thanks :-)

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    back to the top

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VBA - Code to compare 2 columns

    I wonder who wrote this code?....mmmmmm......can you upload a sample workbook with what you want to compare between column A and B and a sample output in column C - this code is using arrays and regular expressions to compare values and if you are a new to coding this would be very difficult for you to understand. If you upload a sample workbook maybe something simpler for you to understand could be written. Unless you want to undergo a crash course in VBA it would be easier to perhaps see what you are trying to do.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: VBA - Code to compare 2 columns

    Hi,

    Here is a good start to understanding the code
    http://www.cpearson.com/excel/Collec...ctionaries.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Contributor
    Join Date
    05-25-2012
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    194

    Re: VBA - Code to compare 2 columns

    In addition to all the previous comments - might also just be a simple excel formula solution for your comparisons - if you described what you wanted or provided a sample workbook I'm sure we could figure out the best method for you.

  6. #6
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    Jindon wrote this code. It works like a charm when I am comparing two columns, but unfortunately I can't tweak it in case I need to use the code in another workbook/increase the number of columns up for comparison.

    I have attached a sample workbook and an explanation inside. Thanks for the replies!
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VBA - Code to compare 2 columns

    I know who wrote the code - sorry I was being facetious. Jindon is an excellent coder and loves Scripting.Dictionary. He has contributed literally thousands and thousands of posts to many Excel forums so he knows exactly what he is talking about. It is just that I have been around long enough to recognise someone's coding style. Anyway, as I explained in a previous post Scripting dictionary, arrays and understanding regular expressions may be a steep learning curve depending on your VBA coding ability. Here is some code which does the same however you may be able to modify it if you require addition of columns or requirements change
    Please Login or Register  to view this content.
    Jindons code is more succinct and works well however this will allow you to perhaps see the logic a little more easily and allow you to manipulate if required.
    Attached Files Attached Files
    Last edited by smuzoen; 07-03-2012 at 11:36 PM.

  8. #8
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VBA - Code to compare 2 columns

    Sorry - in retrospect I should have commented the code
    Please Login or Register  to view this content.
    Last edited by smuzoen; 07-04-2012 at 12:17 AM.

  9. #9
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    Yup, the code he provided looked simple but I could not make minor modifications to it (even after hours of googling). I'll take a look at the sample you provided tonight. The office runs Excel 2003 and I can't install the compatibility package for some reason. Thanks for the help.

    I won't be marking this thread 'Solved' just yet so that I can post some questions/problems I run into when adapting the sample.

  10. #10
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VBA - Code to compare 2 columns

    As you use Excel 2003 here is the xls version
    Attached Files Attached Files

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: VBA - Code to compare 2 columns

    See........
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    Sorry for not replying to this thread, I repped those I could. Smuzoen, thanks for the code. It's a lot easier to understand what's going on with your method but the regex method is cleaner and it seems there are less lines to change if it ever needs modification.

    So I'm getting back to this project after a month's break... Ran through the whole list and found some errors.

    I need this part changed yet again:
    Please Login or Register  to view this content.
    CAF076-1018, CAF076-101-- and CAF076-1019M are all basically the same items. Is there a way to change the pattern such that it only compares up to the third character after the hyphen and not the whole line? I tried this
    Please Login or Register  to view this content.
    but I don't think its any different from the current code.

  13. #13
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    o well, guess I'm sol.

    edit: disregard post.
    Last edited by Swiss Cheese; 08-07-2012 at 01:23 AM.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: VBA - Code to compare 2 columns

    Who are you talking to?

  15. #15
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VBA - Code to compare 2 columns

    I assume the OP is talking to you Jindon as OP would prefer to use regex - that is my reading of thread that is why I did not respond.

  16. #16
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: VBA - Code to compare 2 columns

    Hi Guys

    The OP started a new thread which I closed for being a duplicate. I then received a PM from him basically saying the new thread request was sufficiently different to warrant a new thread. I re-opened the new thread but told him that adding a post here would bump the original thread up and would have a better chance of bringing your responses.

    Here's the new thread: http://www.excelforum.com/excel-prog...21#post2885121

  17. #17
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    Quote Originally Posted by smuzoen View Post
    I assume the OP is talking to you Jindon as OP would prefer to use regex - that is my reading of thread that is why I did not respond.
    Quote Originally Posted by jindon View Post
    Who are you talking to?
    I created another thread to open the question to all the gurus here.

    The question is directed at anyone who can help me with a new regex pattern.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: VBA - Code to compare 2 columns

    I thought so, coz I never found any appreciation from the op not only this thread I have helped.

  19. #19
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    Not sure if serious.

    I added to your reputation (with a thanks comment) for the help because I didn't want to clutter the thread with posts not really related to the topic at hand. Also did the same for Smuzoen. Funny how it says "you cannot give reputation for the same post twice" but shows that I haven't given out any thanks/likes. Also thanked you in ozgrid before I moved here.

    Meh.

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: VBA - Code to compare 2 columns

    Are you kidding?

    No record in my reputation list from you at all.

  21. #21
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    No I am not kidding. Repped both you and Smuzoen. If it didn't get through for whatever reasons, then I'm sorry but I know I did. I also thanked you in that other thread.

    Now please stop derailing this thread.

  22. #22
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    Bump to the top

  23. #23
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: VBA - Code to compare 2 columns

    Do you want a hand with the Regex? If so I can help you however Jindon is great at this stuff and it his original work so I don't want to overpost him as its bad manners. if you get stuck I can expand on his work but will give Jindon first response.

  24. #24
    Registered User
    Join Date
    07-01-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: VBA - Code to compare 2 columns

    Hi Anthony, today is my last day interning at this company and I don't have access to these Excel files from home. The excel project was one of the assignments I had to do in my few months here. If the company thinks they'll be needing the macro, I'm sure they'll be able to get someone else to work on it in the future..

    Thanks for all those who pitched in ITT.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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