PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Delete All but One Duplicate Row in Worksheet-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] Delete All but One Duplicate Row in Worksheet

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Resolved [RESOLVED] Delete All but One Duplicate Row in Worksheet

    I have a single worksheet with (let's say, for simplicity's sake) fifteen rows and ten columns. Rows number 2, 6 and 9 are exact duplicates of each other in all cells. How can I search through all of the rows, find the duplicates, and delete all but one of them (essentially keeping only one of each duplicate row? Below is some code I found that deletes ALL duplicate rows, but I want to keep one of them... Any suggestions?

    Code:
    'Delete Duplicate Rows
          For i = 1 To Worksheets("Overview").UsedRange.Rows.Count
              For j = Worksheets("Overview").UsedRange.Rows.Count To 1 Step -1
                  If Worksheets("Overview").Cells(i, 1) = Worksheets("Overview").Cells(j, 1) Then Worksheets("Overview").Rows(j).Delete
              Next j
          Next i

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,849

    Re: Delete All but One Duplicate Row in Worksheet

    try
    vb Code:
    1. For i = 1 To Worksheets("Overview").UsedRange.Rows.Count
    2.           For j = Worksheets("Overview").UsedRange.Rows.Count To i + 1 Step -1
    3.               If Worksheets("Overview").Cells(i, 1) = Worksheets("Overview").Cells(j, 1) Then Worksheets("Overview").Rows(j).Delete
    4.           Next j
    5.       Next i
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Delete All but One Duplicate Row in Worksheet

    That works BRILLIANTLY. THank yoU!

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width