Consulting

Results 1 to 5 of 5

Thread: Delete if duplicate

  1. #1

    Smile Delete if duplicate

        Dim rCell As Range
         
        For Each rCell In Sheets("BCDU").Range("BCDU12_Duplicates").Cells
             
            If UCase(rCell.Value) = "Duplicate" Then
                rCell.EntireRow.Delete Shift:=xlUp
            End If
             
        Next
    I have the above good to look at the sheet BCDU range BCDU12_Duplicates and go through the range until you find the word duplicate the delete the entire row, keep going till the end of the range...

    have i done something wrong pleae help thanks

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Is BCDU12_Duplicates a column?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Hi mate it's a column (named range)

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Your main problem is that "Duplicate" is not UCASE in your code. The code will not clear all Duplicate values unless you loop from the bottom up. Filter is a better solution

    [VBA]
    Option Compare Text
    Sub DelDups()

    Sheets("BCDU").Range("BCDU12_Duplicates").AutoFilter Field:=1, Criteria1:="Duplicate"
    Sheets("BCDU").Range("BCDU12_Duplicates").SpecialCells(xlCellTypeVisible).E ntireRow.Delete

    End Sub
    [/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

    Enhancment required

    Firstly your a genuis, nice one mate works like a treat.

    one problem, the filter is always place on the row below the header and hence filters that first result to be deleted as well.

    I have a macro which runs though column a:iv and uses the headers for the named ranges. when I run your filter instead of using row where the headers are it uses row 2

        Dim oneCell As Range
        Dim Lastrow As Long
        Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
         
        For Each oneCell In ThisWorkbook.Sheets("O_BCDU").Range("A1:V1")
            With oneCell
                If CStr(.Value) <> vbNullString Then
                    .Offset(1, 0).Resize(Lastrow - 1, 1).Name = CStr(.Value)
                End If
            End With
        Next oneCell
    its only 1 result lost but it would be good if i didnt loose it,

Posting Permissions

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