Loop through cells not completing properly

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to loop through column A and identify any cell which is less than 30 days ago, (the cells contain dates);

VBA Code:
MaxDate = Date - 30

LastRow = Sheet5.Range("A65000").End(xlUp).row
For Each Cell In Sheet5.Range("A2:A" & LastRow)

If Cell.Value >= MaxDate Then

Cell.EntireRow.Delete

End If
Next

It's working, sort of, but doesn't do every cell, I have to run it several times to get it to do every cell - for info there are about 10000 rows of data but not sure if that would make a difference?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How about
VBA Code:
Dim Rng As Range
MaxDate = Date - 30

LastRow = Sheet5.Range("A65000").End(xlUp).Row
For Each cell In Sheet5.Range("A2:A" & LastRow)
   
   If cell.Value >= MaxDate Then
      If Rng Is Nothing Then Set Rng = cell Else Set Rng = Union(Rng, cell)
   End If
Next
If Not Rng Is Nothing Then Rng.EntireRow.Delete
 
Upvote 0
Solution
That is because when you are deleting or inserting rows within a loop, you need to work in reverse order (start from the bottom and go up). Otherwise, the range is constantly shifting as you are trying to loop through it and can cause rows to get missed (i.e. if you rows 4 and 5 need to be deleted, and you are currently on row 4, when you delete row 4, the old row 5 becomes the new row 4 -- however the next row in the loop is 5, so the "new" row 4 gets missed).

Try this:
VBA Code:
Application.ScreenUpdating = False

MaxDate = Date - 30

LastRow = Sheet5.Range("A65000").End(xlUp).row

For r = LastRow to 2 Step -1
    If Sheet5.Cells(r,"A") >= MaxDate Then
        Sheet5.Rows(r).Delete
    End If
Next r

Application.ScreenUpdating = True
 
Upvote 0
Fluff, as usual you come to the rescue - that works just fine.

Thanks for this one and every other little conundrum I've thrown at you this year.

Happy New Year!
 
Upvote 0
That is because when you are deleting or inserting rows within a loop, you need to work in reverse order (start from the bottom and go up). Otherwise, the range is constantly shifting as you are trying to loop through it and can cause rows to get missed (i.e. if you rows 4 and 5 need to be deleted, and you are currently on row 4, when you delete row 4, the old row 5 becomes the new row 4 -- however the next row in the loop is 5, so the "new" row 4 gets missed).

Try this:
VBA Code:
Application.ScreenUpdating = False

MaxDate = Date - 30

LastRow = Sheet5.Range("A65000").End(xlUp).row

For r = LastRow to 2 Step -1
    If Sheet5.Cells(r,"A") >= MaxDate Then
        Sheet5.Rows(r).Delete
    End If
Next r

Application.ScreenUpdating = True
Got it, and understand now - thanks Joe!
 
Upvote 0
You are welcome.
Glad we were able to help you!
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

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