[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Compare and Remove Duplicates between Two Excel Sheets

Posted on 2014-03-10
5
Medium Priority
?
2,097 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

830 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