Solved

Compare and Remove Duplicates between Two Excel Sheets

Posted on 2014-03-10
5
1,773 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Excel compare strings 6 47
Why Won't My Excel Workbook Refresh The Data 2 35
create different version if file exists 1 39
Cannot locate cell 15 37
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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 create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

738 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