have macro which delete row based on if not todays date get delete method range class failed

SWAY14

New Member
Joined
Jul 27, 2022
Messages
15
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
  2. MacOS
Hello,
So I have a macro which deletes the entire row if the date is less than current date. it works fine but the thing is on windows machine I get "delete method of range class failed" since if there is cells with that do not have todays date. But I need it to get out of the loop if this is the case not throw an error. on Mac I do not have this issue.

My code
Sub STEP2()
Application.DisplayAlerts = False
Dim cell As Range
For Each cell In Worksheets("Sheet2").Range("A1:A1000")
If cell.Value < Date Then
cell.EntireRow.Delete
End If
Next cell

End Sub
Any help with be grateful
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hello Sway14,

I'd be surprised if that type of loop construct would work correctly, even with a Mac. If you're deleting rows using a loop construct, then you need to work from the bottom up, as follows:-

VBA Code:
Option Explicit
Sub STEP2()

Application.ScreenUpdating = False
       
          Dim i As Long, lr As Long, ws2 As Worksheet
          Set ws2 = Sheets("Sheet2")
          lr = ws2.Range("A" & Rows.Count).End(xlUp).Row
         
          For i = lr To 2 Step -1
                      If ws2.Cells(i, 1).Value < Date Then
                      ws2.Cells(i, 1).EntireRow.Delete
                End If
          Next i

Application.ScreenUpdating = True

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hello Sway14,

I'd be surprised if that type of loop construct would work correctly, even with a Mac. If you're deleting rows using a loop construct, then you need to work from the bottom up, as follows:-

VBA Code:
Option Explicit
Sub STEP2()

Application.ScreenUpdating = False
      
          Dim i As Long, lr As Long, ws2 As Worksheet
          Set ws2 = Sheets("Sheet2")
          lr = ws2.Range("A" & Rows.Count).End(xlUp).Row
        
          For i = lr To 2 Step -1
                      If ws2.Cells(i, 1).Value < Date Then
                      ws2.Cells(i, 1).EntireRow.Delete
                End If
          Next i

Application.ScreenUpdating = True

End Sub

I hope that this helps.

Cheerio,
vcoolio.
Thank you very much for the feedback and for the great help this worked amazing.
 
Upvote 0
You're welcome Sway. I'm glad to have been able to assist and thanks for the feed back.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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