+ Reply to Thread
Results 1 to 4 of 4

Remove row if duplicate ID# in column

Hybrid View

  1. #1
    Registered User
    Join Date
    12-07-2012
    Location
    WA, USA
    MS-Off Ver
    Excel 2010
    Posts
    8

    Remove row if duplicate ID# in column

    I found the following elsewhere which works almost flawlessly, 'cept it removes the rows that I have already modified. How do I modify this so it removes the second row instead of the first?
    Sub DelRowsColASame() s
        Dim Lastcell As Range 
        Dim I As Long 
         
        Set Lastcell = Cells.Find("*", Searchdirection:=xlPrevious) 
         
        If Not Lastcell Is Nothing Then 
             'sort the list
            Range(Cells(1, 1), Cells(Lastcell.Row, Lastcell.Column)).Sort _ 
            Cells(1, 1), xlAscending, Header:=xlYes 
             'delete out a previous row if 1st column matches
            For I = Lastcell.Row To 2 Step -1 
                If Cells(I, 1).Value = Cells(I - 1, 1).Value Then 
                    Cells(I - 1, 1).EntireRow.Delete 
                End If 
            Next I 
        End If 
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Remove row if duplicate ID# in column

    Usta,

    Welcome to the Excel Forum.


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    
    Option Explicit
    Sub DelRowsColASame()
    Dim Lastcell As Range
    Dim I As Long
    Set Lastcell = Cells.Find("*", Searchdirection:=xlPrevious)
    If Not Lastcell Is Nothing Then
      'sort the list
      Range(Cells(1, 1), Cells(Lastcell.Row, Lastcell.Column)).Sort Cells(1, 1), xlAscending, Header:=xlYes
      'delete out a previous row if 1st column matches
      For I = Lastcell.Row To 2 Step -1
        If Cells(I, 1).Value = Cells(I - 1, 1).Value Then
          Cells(I, 1).EntireRow.Delete
        End If
      Next I
    End If
    End Sub
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Registered User
    Join Date
    12-07-2012
    Location
    WA, USA
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Remove row if duplicate ID# in column

    Thanks! That worked perfectly!

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Remove row if duplicate ID# in column

    Usta,

    You are very welcome. Glad I could help.

    Thanks for the feedback.

    Come back anytime.

+ 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