Solved

Compare and Remove Duplicates between Two Excel Sheets

Posted on 2014-03-10
5
1,525 Views
Last Modified: 2014-03-25
I know how to create a macro to delete dups on the same sheet, but I need one to remove from different sheets.

I have two identical sheets, but I need them to stay seperate.  I would like to to compare Sheet One to Sheet Two using columns B, C, D K.  If there are duplicates, I would like them removed from Sheet One Only.

I would like this done using VBA.
0
Comment
Question by:kateebebe
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 39918197
Do you know how to record macros? I'd run a macro to add a formula columns with a COUNTIFS formula in sheet1 (to catch duplicates), then do an autofilter to remove the duplicate rows.

Something like the following:

Sub Macro1()
    
Dim lCol As Long
Dim lLastRow As Long, lRowLoop As Long, lLastRowSheet2 As Long

lCol = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    
lLastRow = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row

lLastRowSheet2 = Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Row

With Sheets("Sheet1")
    .Range(.Cells(2, lCol), .Cells(lLastRow, lCol)).FormulaR1C1 = "=COUNTIFS(Sheet2!R1C2:R" & lLastRowSheet2 & "C2,RC2," & _
                                                                            "Sheet2!R1C3:R" & lLastRowSheet2 & "C3,RC3," & _
                                                                            "Sheet2!R1C4:R" & lLastRowSheet2 & "C4,RC4," & _
                                                                            "Sheet2!R1C11:R" & lLastRowSheet2 & "C11,RC11)"
    
    
    
    .Range(.Cells(2, lCol), .Cells(lLastRow, lCol)).NumberFormat = "0"
    
    With .Cells(1, 1).CurrentRegion
        .AutoFilter Field:=lCol, Criteria1:=">0"
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
    
    .Range(.Cells(2, lCol), .Cells(lLastRow, lCol)).ClearContents
End With

End Sub

Open in new window

0
 

Author Comment

by:kateebebe
ID: 39918733
Thank you for the alternative idea for how to do this.  Makes sense.  Thank you.

I actually ended up creating a crude formula that was =b2&c2&d2&k2 in each sheet, then a 2nd formula (vlookup with the word "Duplicate") on my main page that compared the two based on that cell and then I ran a delete rows macro based off the results.

Was hoping there was an easy way to compare the two and delete duplicates and this way is pretty crude, but at the end of the day, it did the job I guess.

Thanks for your help.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39918823
Do you need additional help at this stage, or are you fine with current state of things?

Thomas
0
 

Author Comment

by:kateebebe
ID: 39954685
My apologies.  I thought I had accepted your solution already.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39954690
No problem, have fun coding

Thomas
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now