VBA to delete rows if dates in a column match for the whole week

asad

Well-known Member
Joined
Sep 9, 2008
Messages
1,434
Hello All,

I am trying to make a macro work for me. I found a code on a website and tried to modify it to suit my need. The modified code is as follows:
Code:
Sub delrow()Dim row As Long
Dim lastrow As Long
Dim mindate As Date
Dim maxdate As Date


mindate = CDate(InputBox("Please enter start date in dd/mm/yyyy format", vbOKOnly))


maxdate = CDate(InputBox("Please enter last date in dd/mm/yyyy format", vbOKOnly))


lastrow = Cells(Rows.Count, "D").End(xlUp).row
row = 1
Do While row <= lastrow
[COLOR=#ff0000]transdate = CDate(Cells(row, 4)[/COLOR])
    If transdate < mindate Or transdate > maxdate Then
        Rows(row).EntireRow.Delete
    Else
        row = row + 1
    End If
Loop
End Sub
But when I try to run it, the row with red fonts gets highlighted and code stops.
All I am trying to do is to get user to either input a date range for the whole week, or if you guys think it better for the user to enter only start date for the week, then I am happy with that too. But after getting the date, I want to check in column D for all the rows that have any date within that week and delete the whole rows.
Your help will be greatly appreciated.

Thanks
Asad
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I have changed the code a bit. Now it is going into endless loop :eek:
Code:
Sub delrow()Dim row As Long
Dim lastrow As Long
Dim mindate As Date
Dim maxdate As Date


mindate = InputBox("Please enter start date in dd/mm/yyyy format", vbOKOnly)


maxdate = InputBox("Please enter last date in dd/mm/yyyy format", vbOKOnly)


lastrow = Cells(Rows.Count, "D").End(xlUp).row
row = 1
Do While row <= lastrow
transdate = Cells(row, 4)
    If transdate > mindate Or transdate < maxdate Then
        Rows(row).EntireRow.Delete
    Else
        row = row + 1
    End If
Loop
End Sub
 
Upvote 0
Thanks for looking at it. My brother fixed it for me :)

Here is the code if someone wants.
Code:
Sub delrow()Dim row As Long
Dim lastrow As Long
Dim mindate As Date
Dim maxdate As Date


mindate = InputBox("Please enter start date in dd/mm/yyyy format", vbOKOnly)


maxdate = InputBox("Please enter last date in dd/mm/yyyy format", vbOKOnly)


lastrow = Cells(Rows.Count, "D").End(xlUp).row
row = 2
Do While row <= lastrow
transdate = Cells(row, 4)
    If transdate > mindate Or transdate < maxdate Then
        Rows(row).EntireRow.Delete
    End If
    row = row + 1
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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