Macro for Removing Dates 11 Days in Future

EmilyElle

New Member
Joined
Oct 13, 2019
Messages
3
Hello,

I'm trying to create a macro that would do the following functions - can you please advise what the code would be? Thank you.


  • Keep any row with a value in column D
  • Remove any rows where column D is blank and is 11 days out in column G
  • Remove any rows where date is 11 days out in column G
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
.
Code:
Option Explicit


'Keep any row with a value in column D
'Remove any rows where column D is blank and is 11 days out in column G
'Remove any rows where date is 11 days out in column G


Sub DeleteRows()


Dim i As Integer
Dim Rng As Range
Dim WorkRng As Range    ', WorkRng2 As Range
Dim xRows As Long
Dim nDate As Date


Set WorkRng = Sheets("Sheet1").Range("D1:D200")     'edit range here


nDate = Date
xRows = WorkRng.Rows.Count
Application.ScreenUpdating = False


For i = xRows To 2 Step -1
    If WorkRng.Rows(i).Value = 0 And WorkRng.Rows(i).Offset(0, 3).Value = nDate + 11 Then WorkRng.Rows(i).EntireRow.Delete XlDeleteShiftDirection.xlShiftUp
    If WorkRng.Rows(i).Offset(0, 3).Value = nDate + 11 Then WorkRng.Rows(i).EntireRow.Delete XlDeleteShiftDirection.xlShiftUp
Next


Application.ScreenUpdating = True
End Sub
 
Upvote 0
I'm showing that it's deleting some rows but not all the ones. If you filter the blanks in column D there should be nothing past 11 days from today's date. Maybe I'm doing something wrong on my end?

.
Code:
Option Explicit


'Keep any row with a value in column D
'Remove any rows where column D is blank and is 11 days out in column G
'Remove any rows where date is 11 days out in column G


Sub DeleteRows()


Dim i As Integer
Dim Rng As Range
Dim WorkRng As Range    ', WorkRng2 As Range
Dim xRows As Long
Dim nDate As Date


Set WorkRng = Sheets("Sheet1").Range("D1:D200")     'edit range here


nDate = Date
xRows = WorkRng.Rows.Count
Application.ScreenUpdating = False


For i = xRows To 2 Step -1
    If WorkRng.Rows(i).Value = 0 And WorkRng.Rows(i).Offset(0, 3).Value = nDate + 11 Then WorkRng.Rows(i).EntireRow.Delete XlDeleteShiftDirection.xlShiftUp
    If WorkRng.Rows(i).Offset(0, 3).Value = nDate + 11 Then WorkRng.Rows(i).EntireRow.Delete XlDeleteShiftDirection.xlShiftUp
Next


Application.ScreenUpdating = True
End Sub
 
Upvote 0
.
Hmm ... would you be ok posting your workbook to a cloud site like DropBox.com or similar ?

Provide the download link here.
 
Upvote 0

Forum statistics

Threads
1,214,399
Messages
6,119,279
Members
448,884
Latest member
chuffman431a

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