Deleting all rows in sheet that do not match specified date range using VBA

aldousjg

New Member
Joined
Jan 20, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am creating a macro to delete all rows from a sheet that do not meet a specified date range.

The sheet to delete from is named "PreparedData". The start date and end date of the range are specified in cells G5 and I5 respectively in sheet "Macro". Within the "PreparedData" sheet, the dates are in column BP.

This is what I have so far:

Dim lRow As Long
Dim iCntr As Long

lRow = Sheets("PreparedData").UsedRange.Rows.Count

For iCntr = lRow To 1 Step -1
If Cells(iCntr, BP) < Sheets("Macro").Range("G5").Value Then
Rows(iCntr).Delete
Else
If Cells(iCntr, BP) > Sheets("Macro").Range("G5").Value Then
Rows(iCntr).Delete
End If

I know that this loop is wrong, would anyone be able to help to get this to work?

Thankyou
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
VBA Code:
For iCntr = lRow To 1 Step -1
   If Cells(iCntr, "BP") < Sheets("Macro").Range("G5").Value Or Cells(iCntr, "BP") > Sheets("Macro").Range("I5").Value Then Rows(iCntr).Delete
Next
 
Upvote 0
Welcome to the Board!

Two things:
1. If BP is your column reference, it need to be enclosed in double-quotes, i.e. "BP"
2. Your second IF should be referencing I5, not G5 (otherwise, you are deleting ALL rows, except for those exactly equal to G5).

You can simplyify it all down to this:
VBA Code:
Sub MyDeleteRows()

    Dim lRow As Long
    Dim iCntr As Long

    Application.ScreenUpdating = False

    lRow = Sheets("PreparedData").UsedRange.Rows.Count

    For iCntr = lRow To 1 Step -1
        If (Cells(iCntr, "BP") < Sheets("Macro").Range("G5").Value) Or (Cells(iCntr, "BP") > Sheets("Macro").Range("I5").Value) Then
            Rows(iCntr).Delete
        End If
    Next iCntr
        
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
You are welcome.

Glad we could help! :)
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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