Consulting

Results 1 to 12 of 12

Thread: Deleting identical rows between sheets?

  1. #1

    Deleting identical rows between sheets?

    Hi all, I'm trying to create an Excel macro that automatically deletes any rows in Worksheet1 that are also on Worksheet2. Individual cells that match on both sheets don't matter, all that matters is if entire rows match between sheets. Could anyone help me with the code? I'm having a lot of trouble with it

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi,
    Welcome to VBAX
    Can you post your workbook and your code to date. Use Manage Attachments in the Go Advanced section
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Interesting quandry. Curious, MD, have you given though to how to approach it yet?

    Depending on how many columns of data there are, I would probably do something like the following:
    -Apply a formula to Sheets 1 & 2 like =A1&B1&C1&D1...
    -Apply a formula to Sheet 2 that does a vlookup of Sheet2 against Sheet1 and returning True if found and False if not
    -Sort the list by the lookup column (to avoid contiguous range issues later)
    -Filter the list on Sheet 2 to show only the True values (duplicates) and delete them
    -Remove the helper columns

    Of course... seeing the data might change that opinion.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Ken,
    I was hoping for real data to test it on. My tentative solution is to search for initial values, check for range lengths/ number of data cells, and if these are the same, an item by item comparison.
    Might be a bit slow for large numbers though.

    [vba]Option Explicit
    Sub Check()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim Rw1 As Long, i As Long
    Dim FirstAddress As String
    Dim a As Range, c As Range

    'Create a variable reference to the relevant worksheets
    Set ws1 = Sheets(1)
    Set ws2 = Sheets(2)
    'Find the last used row on sheet 1
    Rw1 = ws1.Cells.SpecialCells(xlCellTypeLastCell).Row

    'Step through the cells in Sheet 1 column A, starting from the bottom
    For i = Rw1 To 1 Step -1
    'Search for the value in sheet 2; find all occurences
    With ws2.Columns(1)
    Set a = Cells(i, 1)
    Set c = .Find(a, LookIn:=xlValues)
    If Not c Is Nothing Then
    FirstAddress = c.Address
    Do
    'When value found, pass cell references to function for comparison
    If CompareCells(a, c) = True Then
    'If identical, then delete Sheet 1 row
    a.EntireRow.Delete
    'Exit and go to next cell in Column A
    Exit Do
    End If
    Set c = .FindNext(c)
    'Find next value until first cell is found again
    Loop While Not c Is Nothing And c.Address <> FirstAddress
    End If
    End With

    Next
    End Sub

    Function CompareCells(a As Range, c As Range) As Boolean
    'Create variables to refer to corresponding rows
    Dim aa As Range, cc As Range
    Dim i As Long
    'Set initial value to True
    CompareCells = True
    'Create range of used cells in row on Sheet 1
    Set aa = Range(a, Cells(a.Row, Columns.Count).End(xlToLeft))
    'Create range of used cells in row on Sheet 2
    With Sheets(2)
    Set cc = Range(c, .Cells(c.Row, Columns.Count).End(xlToLeft))
    End With
    'In the number of cells differ or filled cells differ then Compare = False
    With Application.WorksheetFunction
    If .CountA(aa) <> .CountA(cc) Or _
    aa.Cells.Count <> cc.Cells.Count Then
    'Return value and exit function
    CompareCells = False
    Exit Function
    End If
    End With
    'Compare both ranges cell by cell
    For i = 1 To aa.Cells.Count
    'If compare fails then Compare = False; exit function
    If aa(i) <> cc(i) Then
    CompareCells = False
    Exit Function
    End If
    Next
    End Function

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Don't know if this will help and I don't remember where I got it...have had it since before I was a member here. Code is not protected.

    I use it to find differences in sheets..maybe you can get some ideas from it.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If the top lines of each sheet are the same (like a header row). This will delete any rows on sheet1 that are duplicated on sheet2. (It deletes that duplicated top row on sheet1 only.)
    Sheets("sheet1").UsedRange.AdvancedFilter Action:=xlFilterInPlace, _
            CriteriaRange:=Sheets("sheet2").UsedRange, _
            Unique:=False
    Sheets("sheet1").SpecialCells(xlCellTypeVisible).Delete

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Well now, that is a nice approach!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Ken Puls
    Well now, that is a nice approach!
    Agreed, but I can't get it to work with large amounts of data (5000 rows x 6 columns)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    mdmackillop, your code works great for me, thank you very much.

    Also, I am pretty new to VBA and am eager to gain a better understanding of it. If you have a few minutes, I would really appreciate if you could explain to me in layman's terms what your code means and what it's doing exactly. Thanks!

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by wakingedge42
    mdmackillop, your code works great for me, thank you very much.

    Also, I am pretty new to VBA and am eager to gain a better understanding of it. If you have a few minutes, I would really appreciate if you could explain to me in layman's terms what your code means and what it's doing exactly. Thanks!
    I've added comments to my previous post.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    mdmackillop, thanks a lot for the comments, they are very helpful.

    but some bad news: i tried your macro with my financial reports (which i can't post unfortunately because they contain confidential info.) and it didn't work. my reports are about 50 rows x 16 columns. apparently your code works great for a small data set, but for lots of data, it doesn't work at all. got any ideas on how to fix this?

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Deleting too much or nothing at all? You'll need to test on known data/results and then step through problem lines to track down the error. The code logic is pretty simple.

    Try adding some colour eg
    [VBA]If aa(i) <> cc(i) Then
    CompareCells = False
    aa(i).interior.colorindex = 6
    Exit Function
    End If [/VBA]
    Use this to check equal or nonequal values
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •