+ Reply to Thread
Results 1 to 9 of 9

Code to compare data in 2 sheets and delete duplicates

  1. #1
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Code to compare data in 2 sheets and delete duplicates

    Hi everyone,
    As you can read from my title, I am trying to setup a macro that will compare the information on 2 sheets, delete the duplicates and finally paste the remaining values on another sheet (On the attached WB it needs to be pasted on the 'Difference' sheet)
    I usually run reports @ 2, 4 and 6 pm. The 4 pm contains everything that was on the 2 pm report + any updates that happened between 2 & 4 pm. Same applies for the 6 pm.

    I found a code online that allows me to find duplicates from the 2 and 4 pm reports and delete it from the 4 pm sheet. Here it is:

    Please Login or Register  to view this content.
    However, instead of deleting the values from the actual report, I would like to run the macro from the 'Difference' sheet and the former pastes the data on the sheet.
    I would also like to prevent the first row from being deleted.

    Is that possible? If it is, any help would be much appreciated.

    Thanks,
    Amar.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to compare data in 2 sheets and delete duplicates

    "However, instead of deleting the values from the actual report, I would like to run the macro from the 'Difference' sheet and the former pastes the data on the sheet."

    Currently the code compares sheets 2 and 4 based on column A. If they match, the rows in sheet 4 are deleted. The code does not copy to any sheet.
    You need to explain what change do you want to make?
    "deleting the values from the actual report"
    Do you mean deleting from sheet 4pm?

    How can you run the macro from the difference sheet when that sheet is blank?
    Not clear!

  3. #3
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: Code to compare data in 2 sheets and delete duplicates

    Hi AB33,
    Sorry if I wasn't clear.
    So I run the report @ different times and then paste the data in the appropriate sheet in the attached workbook. The 4 pm report has the 2 pm's data +any update, and the 6 pm report has the 2 pm and 4 pm data + any updates that happened between 4 and 6 pm.
    Let's say I want to find updates that occured between 2 pm and 4 pm. I run the macro, it finds the updates and it deletes the duplicates from the 4 pm sheet. Now if I want to find updates between 4 and 6 pm, I will make the macro compare 4 and 6 pm sheet. However, the results will be scewed because only data fom the 4 pm will be deleted, thus leaving me with 2 pm's data +updates, on the 6 pm sheet.
    I hope this makes sense.
    So instead of deleting the data from the sheets, i would like the macro to find the updates and paste them in on another sheet. If I can get only the code that will do this for the 2 pm and 4 pm sheet, I will be a happy camper.

    Thanks,
    Amar

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to compare data in 2 sheets and delete duplicates

    Do you mean the difference(Missing from sheet2) between sheet2 and 4 are pasted in to sheet difference?

  5. #5
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: Code to compare data in 2 sheets and delete duplicates

    Yes, exactly what I meant.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to compare data in 2 sheets and delete duplicates

    Please see attached my code.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: Code to compare data in 2 sheets and delete duplicates

    This is exactly what I am looking for. Thank you so much.
    There are 2 minor things that I noticed, however. The code removes leading zeros and dashes. Can this be prevented?

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Code to compare data in 2 sheets and delete duplicates

    "The code removes leading zeros and dashes. Can this be prevented"?
    The code uses an array to copy data and so do not see how?
    Which line? It might be a formatting issue with your data.

  9. #9
    Registered User
    Join Date
    08-30-2012
    Location
    Ottawa
    MS-Off Ver
    Excel 2007
    Posts
    59

    Re: Code to compare data in 2 sheets and delete duplicates

    Yeah, it was the formatting...rookie mistake
    Thanks a lot, buddy.

+ 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