VBA fast way to delete rows in a 2-column range if the date is less than a defined date

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
801
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I've defined a "Ddate" as being "Now()+14"

I have a set of data on "Temp" sheet which has 2 columns. A is string and B is a list of dates.

What I need to do is quickly run down these ~3,000 rows and delete any row where the value in B is less than or equal to "Ddate"

I've tried a Do Until loop, but it's incredibly slow.

Thanks!
 

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.
Try
For each cell in range b:b if cell.value = date then set rng =union(rng, cell)
Rng.entirerow.delete
Sorry, but i type this on my phone.
 
Upvote 0
Why not use autofilter & delete the visible rows
 
Upvote 0
try this: using varinat arrays for speed!!
Code:
Sub ddt()
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 2))
Range(Cells(1, 1), Cells(lastrow, 2)) = ""
outarr = Range(Cells(1, 1), Cells(lastrow, 2))
indi = 1
For i = 1 To lastrow
 If inarr(i, 2) > Now() + 14 Then
   outarr(indi, 1) = inarr(i, 1)
   outarr(indi, 2) = inarr(i, 2)
   indi = indi + 1
 End If
Next i
Range(Cells(1, 1), Cells(lastrow, 2)) = outarr
 
Upvote 0
So sorry guys, I've gotten around this by removing the results I didn't want at the filter stage - initially, I tried this and it couldn't seem to filter the date correctly (cutoff was, an example, 09/05/2019 and later [9th May])

But it was filtering from the 10th September onwards, which wasn't right.

Turns out that even though I was giving a date of "dd/mm/yyyy" and the target sheet to be filtered was "dd/mm/yyyy" that it was instead filtering based on "mm/dd/yyyy" and thus not giving me the right answer.

It's all sorted now, thanks for the help though!
 
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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