Consulting

Results 1 to 5 of 5

Thread: basic unique refs loop

  1. #1
    VBAX Regular
    Joined
    Mar 2009
    Posts
    48
    Location

    basic unique refs loop

    Hi

    I am creating a loop that deletes any duplicates. so the logic is to put the data into ascending order then delete an entry if it equals the preceeding:

    nlastrow = Range("A65536").End(xlUp).Row

    Range("A2").Select

    For nloop = 1 To nlastrow

    If InStr(ActiveCell, ActiveCell.Offset(-1, 0)) = 1 Then
    ActiveCell.EntireRow.Delete

    Else: ActiveCell.Offset(1, 0).Select

    End If

    Next nloop


    the problem: the loop i have created is deleting not only identical refs but those with similarities. e.g. row1 has DPC1, row 2 has DPC2. however the current loop is deleting DPC2.

    the instr statement needs to be modified, i thought = 1 meant they had to be identical but i must be wrong.

    can someone advise?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I think it's better to go from the bottom up when deleting rows

    Also not sure why you wanted InStr

    Not tested but maybe something like this

    [vba]
    nlastrow = Range("A65536").End(xlUp).Row


    For nloop = nlastrow to 2 Step -1

    If Cells(nloop,1).Value = Cells(nloop-1,1).Value Then
    Rows(nloop).Delete
    End If

    Next nloop
    [/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    When dealing with duplicates, I always reach for this.
    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'

  4. #4
    VBAX Regular JONvdHeyden's Avatar
    Joined
    Mar 2009
    Location
    Hampshire, UK
    Posts
    75
    Location
    Personally I prefer to use a filter method when deleting rows based on criteria. Either AutoFilter or Advanced Filter. Filter methods tend to be substantially quicker when dealing with larger ranges.

    [VBA]
    Sub Del_Dupes()

    Dim lRow As Long, rngCrit As Range
    Set rngCrit = Cells(2, Columns.Count).End(xlToLeft).Offset(, 2)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row

    rngCrit.FormulaR1C1 = "=RC1=R[-1]C1"

    With Cells(1, 1).Resize(lRow)
    .AdvancedFilter action:=xlFilterInPlace, criteriarange:=rngCrit.Offset(-1).Resize(2)
    .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With

    ActiveSheet.ShowAllData

    End Sub
    [/VBA]
    Regards,
    Jon von der Heyden (Excel 2003, OS XP Pro)

  5. #5
    VBAX Regular
    Joined
    Mar 2009
    Posts
    48
    Location
    appreciate the help guys. all v useful.


Posting Permissions

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