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

aldousjg

New Member
Joined
Jan 20, 2021
Messages
10
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
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
1,061
Office Version
  1. 2016
Platform
  1. Windows
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,651
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,651
Office Version
  1. 365
Platform
  1. Windows
You are welcome.

Glad we could help! :)
 

Watch MrExcel Video

Forum statistics

Threads
1,127,612
Messages
5,625,839
Members
416,138
Latest member
Pizzaman22

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
Top