+ Reply to Thread
Results 1 to 7 of 7

Macro To Delete Entire Row If Dublicate Is Found

Hybrid View

SilverFox Macro To Delete Entire Row If... 12-06-2012, 12:22 PM
arlu1201 Re: Macro To Delete Entire... 12-06-2012, 01:22 PM
SilverFox Re: Macro To Delete Entire... 12-07-2012, 05:09 AM
Trebor76 Re: Macro To Delete Entire... 12-07-2012, 05:34 AM
arlu1201 Re: Macro To Delete Entire... 12-07-2012, 05:41 AM
SilverFox Re: Macro To Delete Entire... 12-07-2012, 05:57 AM
Trebor76 Re: Macro To Delete Entire... 12-07-2012, 06:07 AM
  1. #1
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    79

    Macro To Delete Entire Row If Dublicate Is Found

    I have a work sheet called (waste data) what i am after is a Macro that will delete an entire row if the data in A, B & G is dublicated anywhere on the entire sheet

    Lets say

    A1 = 5 B1 = Jim G1 = Na41

    A21 = 5 B21 = Jim G21 = Na41

    Row 21 will be deleted as the same data is repeated in Row1

    The same data will be repeated more that once in A, B & G, in this case it will delete them

    Thanks

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro To Delete Entire Row If Dublicate Is Found

    Try this code
    Sub delete_dups()
    Dim i As Long, lrow As Long
    
    Application.ScreenUpdating = False
    
    With Worksheets("Sheet1")
        .Cells.Sort Key1:=.Range("A2"), Order1:=xlAscending, Key2:=.Range("B2") _
            , Order2:=xlAscending, key3:=.Range("G2"), order3:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
           
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = lrow To 2 Step -1
            If .Range("A" & i).Value = .Range("A" & i - 1).Value And .Range("B" & i).Value = .Range("B" & i - 1).Value And _
                .Range("G" & i).Value = .Range("G" & i - 1).Value Then
                .Rows(i).Delete
            End If
        Next i
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose Tools | Macro | Macros
    Select a macro in the list, and click the Run button
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    79

    Re: Macro To Delete Entire Row If Dublicate Is Found

    Hi Arlu

    I have tried your code but keep getting an error, I have inserted a sample sheet for you to look at if you don't mind? The last 2 entries on this sheet are dublicates and one should be deleted

    Thanks for the help
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Macro To Delete Entire Row If Dublicate Is Found

    Hi SilverFox,

    See how this goes:

    Option Explicit
    Sub Macro1()
    
        'Written by Trebor76
        'Visit my website www.excelguru.net.au
    
        Dim objMyUniqueEntries As Object
        Dim lngRowStart As Long, _
            lngRowEnd As Long, _
            lngMyRow As Long, _
            lngMyCounter As Long
        Dim rngDelRange As Range
        
        Set objMyUniqueEntries = CreateObject("Scripting.Dictionary")
        lngRowStart = 2 'Starting row number for the data. Change to suit.
        lngRowEnd = Range("A:X").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        Application.ScreenUpdating = False
        
        For lngMyRow = lngRowStart To lngRowEnd
            If objMyUniqueEntries.exists(CStr(Range("A" & lngMyRow) & Range("B" & lngMyRow) & Range("G" & lngMyRow))) = False Then
                lngMyCounter = lngMyCounter + 1
                objMyUniqueEntries.Add CStr(Range("A" & lngMyRow) & Range("B" & lngMyRow) & Range("G" & lngMyRow)), lngMyCounter
            Else
                If rngDelRange Is Nothing Then
                    Set rngDelRange = Cells(lngMyRow, "A")
                Else
                    Set rngDelRange = Union(rngDelRange, Cells(lngMyRow, "A"))
                End If
            
            End If
        Next lngMyRow
        
        Set objMyUniqueEntries = Nothing
        
        'If the 'rngDelRange' range has been set, then...
        If Not rngDelRange Is Nothing Then
            '...delete the row(s) from it.
            rngDelRange.EntireRow.Delete
            Application.ScreenUpdating = True
            MsgBox "The duplicate rows have now been deleted.", vbInformation, "Delete Duplicate Row Editor"
            Set rngDelRange = Nothing
        'Else...
        Else
            '...display a message that no rows were deleted as they were all unique.
            Application.ScreenUpdating = True
            MsgBox "There were no rows deleted as no duplicates were found.", vbExclamation, "Delete Duplicate Row Editor"
        End If
        
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro To Delete Entire Row If Dublicate Is Found

    What error do you get and which code line is highlighted?

  6. #6
    Registered User
    Join Date
    02-25-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    79

    Re: Macro To Delete Entire Row If Dublicate Is Found

    Hi Trevor thanks very much for this it worked first time, Arlu thanks also, the error i was getting was syntax error, not sure why. Thanks again guys. Problem Solved

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Macro To Delete Entire Row If Dublicate Is Found

    Thanks for letting us know and you're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1