+ Reply to Thread
Results 1 to 13 of 13

[VBA] Comparing two columns and pasting the difference into a third column

  1. #1
    Registered User
    Join Date
    02-04-2012
    Location
    Portland, OR
    MS-Off Ver
    2010
    Posts
    5

    [VBA] Comparing two columns and pasting the difference into a third column

    I'm trying to compare two different columns of products and pasting the different into a third.

    If there's a product in column A but isn't in column B. It will show in Column C.

    Example:

    A | B | C
    ----------
    1 | 2 | 1
    2 | 3 | 2
    3 | 0 | 0
    4 | 5 |


    I've just started to pick up VB recently so I'm still a beginner. Any help would be appreciated. Thanks
    Last edited by hydroid; 02-04-2012 at 10:11 PM.

  2. #2
    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] Comparing two columns and pasting the difference into a third column

    I do not understand the logic. Number 1 is in column A and not in column B so 1 appears in C - makes sense.
    2 appears in column A and in Column B however 2 goes into C - what do you want to appear in C if the number is in Column A and Column B
    3 appears in Column A and in Column B and 0 goes into C - should 0 go in to C is the number is not in Column A and Column B?...because this contradicts 2 being in column A and in Column B.
    I do not see consistent logic - could you upload a sample workbook with very specific rules about what should appear in C based on Values being in Column A and Column B.

    A sample workbook with no private/sensitive data would make things easier.
    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?

  3. #3
    Registered User
    Join Date
    02-04-2012
    Location
    Portland, OR
    MS-Off Ver
    2010
    Posts
    5

    Re: [VBA] Comparing two columns and pasting the difference into a third column

    My mistake. It was meant to be along the lines of:


    A | B | C
    ----------
    1 | 2 | 1
    2 | 3 | 0
    3 | 0 | 5
    4 | 5 | 4

    I'm pretty lost in how I would even begin this entire process.

  4. #4
    Registered User
    Join Date
    02-04-2012
    Location
    Portland, OR
    MS-Off Ver
    2010
    Posts
    5

    Re: [VBA] Comparing two columns and pasting the difference into a third column

    Please Login or Register  to view this content.

    That's what I have compiled so far. But instead of if X = Y. I want it so if X doesn't equal to Y, it would paste into the 3rd column. Just that if I do <> it doesn't work.

  5. #5
    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] Comparing two columns and pasting the difference into a third column

    I get that if the number is in A but not in B then the number in A goes to C. That being said you example above 3 exists in column A and in Column B so should 0 be in C and NOT 5. If the following is correct
    A B C
    1 2 1
    2 3 0
    3 0 0
    4 5 4

    That means if the number is in column A and in column B then 0 (zero) goes into column C - if you could confirm that logic then I can write something for you. If you can as well rather than posting examples in the thread can you please upload sample workbooks (select Go Advanced, Manage Attachments) and you can upload a dummy sample workbook.
    If you confirm the logic above please or explain why 5 is in Column C when 3 exists in Column A and Column B

  6. #6
    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] Comparing two columns and pasting the difference into a third column

    You could do it without VBA using a formula
    =IF(ISERROR(MATCH(A1,$B$1:$B$4,0)),A1,0) - Place in C1 and copy down.
    Do you particularly want a VBA solution to this?

  7. #7
    Registered User
    Join Date
    02-04-2012
    Location
    Portland, OR
    MS-Off Ver
    2010
    Posts
    5

    Re: [VBA] Comparing two columns and pasting the difference into a third column

    I'm looking towards primarily a VBA solution. I want to connect this macro to a button later on in my document.


    To clarify the logic:


    If the number is in A but not in B then the number goes to C. If the number is in B but not in A then it goes to C as well.

    Attached it as well. Thanks for the help a head of time.
    Attached Files Attached Files

  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] Comparing two columns and pasting the difference into a third column

    Your logic still does not make sense
    A B C
    1 2 1 Existing in column A, but not B - Agree 1 exists in Column A but not B so 1 goes into C
    2 3 0 Exists in column B, but not A - 2 exists in Column A and in Column B however you say Exists in Column B but not A.
    3 0 5 Exists in column B, but not A - Why is 5 placed here? 3 Exists in Column A and B however you say it exists in Column B but not A
    4 5 4 Exists in column A, but not B

    I must be missing something. You are checking that a value in A is in column B. I just don't understand your rules. Perhaps someone will understand something I am missing.
    I just have no idea why 5 is in row 3?
    I am not being mean or aggressive I just do not get your logic rules.

    If the number is in A but not in B then the number goes to C. If the number is in B but not in A then it goes to C as well.
    That means that there could be 2 outputs into column C.
    Could you perhaps upload a slightly larger sample in a workbook - it is obviously clear in your mind but I am just not getting it.
    Last edited by smuzoen; 02-05-2012 at 11:45 PM.

  9. #9
    Registered User
    Join Date
    02-04-2012
    Location
    Portland, OR
    MS-Off Ver
    2010
    Posts
    5

    Re: [VBA] Comparing two columns and pasting the difference into a third column

    I've attached another example. Hopefully it clarifies it.

    IE. If Person A appears in first column, but not the second, it will then paste to the third.
    If Person B appears in second column, but not the first, it will then paste to the third.
    If Person C appears in first column and second column, then it will NOT show in the third.
    Attached Files Attached Files

  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] Comparing two columns and pasting the difference into a third column

    I get your logic now - essentially 2 comparisons are required and an array constructed from the comparisons. If you wanted to just construct a unique list in an array you could use Scripting Dictionary and Keys however your request is different. Try this
    Please Login or Register  to view this content.
    See attachment for example.
    Attached Files Attached Files

  11. #11
    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] Comparing two columns and pasting the difference into a third column

    Here is a slightly more elegant solution
    Please Login or Register  to view this content.

  12. #12
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: [VBA] Comparing two columns and pasting the difference into a third column

    @smuzoen,

    Hi Anthony,

    I have been wrestling with this for more than one and a half hours. Tried Array formulas and everything else, with no luck.

    I like what you have done here. It works Perfect !!! Right at the bottom of your code I have added this line:

    Range("C:C").SortSpecial SortMethod:=xlPinYin

  13. #13
    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] Comparing two columns and pasting the difference into a third column

    @Winon
    A good idea - the OP's example was a semi sorted list so this is a nice addition.
    Thanks

+ 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