Link to home
Start Free TrialLog in
Avatar of Lawrence Salvucci
Lawrence SalvucciFlag for United States of America

asked on

Delete Rows where current month is found in column A

I need to be able to delete all rows where the date in column A is within the range in cell R2 & S2.

R2 = Start Date
S2 = End Date

I need to be able to do this via VBA code.
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can do this with this code:

Sub DeleteRows()

    Dim lstrow As Long
    Dim x As Long
   
    CheckDate = DateSerial(Year(Now), Month(Now), 1)
    lstrow = Cells(Rows.Count, 1).End(xlUp).Row

    For x = lstrow To 3 Step -1
        If Cells(x, 1).Value < Range("R2") Or Cells(x, 1) > Range("S2") Then
            Rows(x).Delete
        End If
    Next

End Sub

Open in new window

Avatar of Lawrence Salvucci

ASKER

How do I specify a specific sheet in my workbook in case I want to run this from another file or if I'm on a different sheet within that workbook?

lngLastRow = ActiveSheet.UsedRange.Rows.Count
lngLastRow = Sheets("Sheet1").UsedRange.Rows.Count
Thanks for the help! Much appreciated!
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
@lsalvucci: did you at least tryed my code?!?
This line

If Cells(x, 1).Value < Range("R2") Or Cells(x, 1) > Range("S2") Then

would have deleted only dates outside of the range.