Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Compare and Remove Duplicates between Two Excel Sheets

Posted on 2014-03-10
5
Medium Priority
?
2,009 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

609 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