VBA my simple code not working properly

omersinem

New Member
Joined
Sep 13, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi,
I wrote a code that which allows to delete repetitive data with the same columns and then defined a button for this but I have to click the button more than four or five times to delete the duplicate data. For example; It deletes 5 or 10 rows every time I click it. When the total data also does not decrease the number of rows, then I understand that all matching rows are deleted. I want to delete repetitive rows as stated in the attachment in one click and I want it to auto-loop through newly updated rows as rows are deleted, but I can't. Anyone who can help?
Thanks,
1684331877056.png


Sub y()

Sayfa1.Range("A2").Sort [A2], Order1:=xlDescending


For i = 2 To Sayfa1.UsedRange.Rows.Count
Sayfa1.Range("B2").Sort [B2], Order1:=xlDescending

If Sayfa1.Cells(i, 1) = Sayfa1.Cells(i + 1, 1) Then



If Sayfa1.Cells(i, 2) = Sayfa1.Cells(i + 1, 2) Then


If Sayfa1.Cells(i, 8) = "NOK" And Sayfa1.Cells(i + 1, 8) = "NOK" Then
Sayfa1.UsedRange.Rows(i + 1).Delete
ElseIf Sayfa1.Cells(i, 8) = "OK" And Sayfa1.Cells(i + 1, 8) = "OK" Then
Sayfa1.UsedRange.Rows(i + 1).Delete


End If

End If

End If
Next
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
have you tried starting from the last row and working upwards

or

when you delete a row minus one from your i count
 
Upvote 0
Solution
The issue is that when you delete a row, you shift all your data upwards. So data can get missed, because you are moving an unchecked row up into the row you just checked (skipping over that row).

As a rule of thumb, if looping through rows where you may be inserting or deleting rows within the loop, it is always best to work "from the bottom up" (looping through the rows backwards, from the last to the first).
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,685
Members
449,117
Latest member
Aaagu

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