VBA matchFoundIndex and activecell.entirerow.delete issue

ronnierunfast

New Member
Joined
Dec 19, 2008
Messages
16
Hi all,

I have the following code but it doesn't delete all duplicates, only some. If it is run several times it does eventually remove all duplicates but not on initial running of code.
The code works fine if i replace:
Cells(iCntr, 4).Activate
ActiveCell.EntireRow.Delete
with:
Cells(iCntr, 5) = "Duplicate"

I have also tried:
Cells(iCntr, 4).Offset(-1,0).Activate
ActiveCell.Offset(1,0).EntireRow.Delete
in case it was an issue with looping through after the row was deleted.




Any pointers would be appreciated!
Code:
Sub DeleteDuplicates()


Dim wsweek As Worksheet
Set wsweek = ActiveWorkbook.Worksheets("weekly")
Set week = wsweek.Range("A3")


wsweek.Activate


'Declaring the lastRow variable as Long to store the last row value in the Column1
    Dim LastRow As Long


'matchFoundIndex is to store the match index values of the given value
    Dim matchFoundIndex As Long


'iCntr is to loop through all the records in the column 1 using For loop
    Dim iCntr As Long


'Finding the last row in the Column 1
    LastRow = Range("A65000").End(xlUp).Row


'looping through the column D = 4
    For iCntr = 4 To LastRow
        'checking if the cell is having any item, skipping if it is blank.
        If Cells(iCntr, 4) <> "" Then
            'getting match index number for the value of the cell
            matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 4), Range("D1:D" & LastRow), 0)
            'if the match index is not equals to current row number, then it is a duplicate value
            If iCntr <> matchFoundIndex Then
            
[B][COLOR=#00ff00]            'NEW CODE FOR DELETING DUPLICATES[/COLOR][/B]
[COLOR=#ff0000]            Cells(iCntr, 4).Activate[/COLOR]
[COLOR=#ff0000]            ActiveCell.EntireRow.Delete[/COLOR]


[B][COLOR=#00ff00]             'END OF NEW CODE FOR DELETING DUPLICATES[/COLOR][/B]
             
           
        End If
     End If
    Next
End Sub
 
Last edited by a moderator:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try
Code:
Sub DeleteDuplicates()
   Dim wsweek As Worksheet
   Dim LastRow As Long
   Dim matchFoundIndex As Long
   Dim iCntr As Long
   
   Set wsweek = ActiveWorkbook.Worksheets("weekly")
   Set week = wsweek.Range("A3")
   
   With wsweek
      LastRow = .Range("A65000").End(xlUp).Row
      'looping through the column D = 4
      For iCntr = LastRow To 4 Step -1
         'checking if the cell is having any item, skipping if it is blank.
         If .Cells(iCntr, 4) <> "" Then
            'getting match index number for the value of the cell
            matchFoundIndex = WorksheetFunction.Match(.Cells(iCntr, 4), .Range("D1:D" & LastRow), 0)
            'if the match index is not equals to current row number, then it is a duplicate value
            If iCntr <> matchFoundIndex Then
            'NEW CODE FOR DELETING DUPLICATES
            .Rows(iCntr).Delete
            'END OF NEW CODE FOR DELETING DUPLICATES
            End If
         End If
      Next
   End With
End Sub
 
Upvote 0
Thanks fluff for the fast reply.

it did similar to my code where it didn't delete all duplicates in one go, but using your code i amended it slightly to do what i was trying with offset(1,0):

Rows(iCntr).Delete
iCntr = iCntr - 1

this then deleted all duplicates in one go!!!

thank you!!!
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top