+ Reply to Thread
Results 1 to 6 of 6

Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

  1. #1
    Registered User
    Join Date
    02-21-2014
    Location
    author1
    MS-Off Ver
    author1
    Posts
    2

    Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

    I am a newbie to VBA and could get parts of what I wanted to do with vlookup, but not the complete solution. I will be gratefull for any help on this.

    Essentially, I have a sheet of data (Sheet1 below) running into almost 1000 rows. Each month, I get an updated sheet (Sheet2) which has overlapping data with sheet1 (rows starting with N, O, P, Q), new data (rows starting with R, S) and data present in Sheet1 but not in Sheet2 (row starting with M in Sheet1). The columns "Item" and "Quantity" in sheet1 are populated by me and are blank in sheet2. The table below explains the structure.

    Sheet1
    Name ID Date Item Quantity
    M 231 14/03/2001 egg 5
    N 322 21/03/2002 bread 6
    O 234 22/04/2001 tomato 45
    P 542 16/01/1999 potato 25
    Q 562 31/01/2003 carrot 14

    Sheet2
    Name ID Address Date Item Quantity
    N 322 address1 21/03/2002
    O 234 address2 22/04/2001
    P 542 address3 16/01/1999
    Q 562 address4 31/01/2003
    R 524 address5 16/02/1998
    S 352 address6 02/01/2011

    It gets tedious to update Sheet2 every month. Is it possible to merge the 2 sheets into a Sheet3 (like below) if Name, ID, Date in each row is an exact match?

    Sheet3
    Name ID Date Item Quantity
    M 231 14/03/2001 egg 5
    N 322 21/03/2002 bread 6
    O 234 22/04/2001 tomato 45
    P 542 16/01/1999 potato 25
    Q 562 31/01/2003 carrot 14
    R 524 16/02/1998
    S 352 02/01/2011

    Further, it will make my job much easier, if it was possible to
    • identify rows only present in Sheet1 and add a tag "absent in new" in a new column
    • identify rows only present in Sheet2 and add a tag "new" in the new column
    • identify rows with matching data in Name, ID, Date in Sheet1 and Sheet2 and add a tag "current" in the new column.


    Something like the table below:

    Sheet3 (Desirable)
    Name ID Date Item Quantity Comments
    M 231 14/03/2001 egg 5 Absent in New
    N 322 21/03/2002 bread 6 Current
    O 234 22/04/2001 tomato 45 Current
    P 542 16/01/1999 potato 25 Current
    Q 562 31/01/2003 carrot 14 Current
    R 524 16/02/1998 New
    S 352 02/01/2011 New


    Many thanks for all your help and thanks a lot for reading through this so far.

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

    see attached file three sheets
    source data sheet1 and sheet2
    result of macro sheet3

    two macro in vb edito;sr

    IT IS ENOUGH IF YOU RUN TESTONE REPEAT TESTONE ONLY

    FEEDBACK PLEASE


    the macros are also repeated here.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    I am not an expert. better solutions may be available
    [email protected]

  3. #3
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

    Hi..

    Can you attach a sample Workbook.. sounds like a job for the Scripting.Dictionary..

  4. #4
    Registered User
    Join Date
    02-21-2014
    Location
    author1
    MS-Off Ver
    author1
    Posts
    2

    Re: Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

    @venkat1926 - works brilliantly without a flaw or error. Many thanks for the effort and time. You have essentially replaced this program "http://www.ablebits.com/excel-lookup-tables/index.php".
    Many thanks once again. I have combined steps one and two into a single macro.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-21-2014
    Location
    author1
    MS-Off Ver
    author1
    Posts
    2

    Re: Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

    One of the problems I have noticed with the above: If there is a discrepancy in the data between Sheet1 and Sheet2 in rows other than "Name" the errors are disregarded.

    Can the above be modified to check for the "Name", "ID Number" and "Date" between sheet1 and Sheet2 and then to implement the following please-
    • "current" if all three match in both sheets
    • "Absent in new" if no matching rows for "Name", "ID Number" and "Date" of Sheet1 row in Sheet2
    • "New" if only present in Sheet2


    Thank you once again.

  6. #6
    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: Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

    Hi, author1,

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    http://www.mrexcel.com/forum/excel-q...tions-met.html

    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

  7. #7
    Registered User
    Join Date
    02-13-2013
    Location
    Bergen County
    MS-Off Ver
    Excel 2010
    Posts
    36

    Cool Re: Compare and copy new rows from Sheet2 to Sheet1 if conditions are met

    Hi author1

    For simpler code and solution try this which compares the values on a tier 2 level basis meaning it doesn't just use 1 value as a look up but uses 2 values and can use more if needed. Also it doesn't require copying and paste but uses cell references.

    Feel free to see more Excel examples on my site by clicking on this link.




    Please Login or Register  to view this content.
    thanks,
    Alfonso
    Attached Files Attached Files
    Last edited by fonzie29; 02-28-2014 at 01:41 AM.

+ 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. Replies: 8
    Last Post: 12-22-2013, 01:16 PM
  2. Replies: 6
    Last Post: 07-25-2013, 02:58 PM
  3. How to copy Specefic data from Sheet1 to Sheet2 if conditions met.
    By z-eighty2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2013, 03:44 PM
  4. How to Copy Rows from sheet1 to sheet2
    By seaottersut in forum Excel General
    Replies: 5
    Last Post: 03-14-2013, 04:04 PM
  5. Compare 2 cells on sheet1 with 2 cells on sheet2 and copy unmatched rows to sheet3
    By drewship in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-08-2010, 07:56 AM

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