Forum

vba delete rows bas...
 
Notifications
Clear all

vba delete rows based on date

2 Posts
2 Users
0 Reactions
199 Views
(@rkschroder)
Posts: 1
New Member
Topic starter
 

I'm trying to learn VBA, but I'm really stuck.

Each month new data (from the previous month) is pasted over the old data. The number of days each month changes due to nonwork days. I've tried clearing the cells and pasting data in blank cells, but this messes up the associated charts and graphs. Eventually I will learn how to have VBA create new charts, but I'm not there yet. I want to delete entire rows with data before the last month, day 1.

Example of data

I've read so many posts and I've tried so many ways. I'm just not getting it and I'm losing my mind!

Sub ltest()

Dim d As Date
d = DateAdd("y", -1, Date)
LastMonth = Month(DateAdd("m", -1, Date))

Dim w As Long
For w = Sheet1.[a4].SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
    Debug.Print Cells(w, "A").Value
    If CDate(Cells(w, "A")) < CDate(LastMonth) Then
        Cells(w, "A").EntireRow.Delete
    End If
Next w

End Sub

I hope that I have posted this correctly and I really appreciate any help.
 
Posted : 11/05/2023 6:04 pm
(@keebellah)
Posts: 373
Reputable Member
 

Good morning,

I with defime LastMonth as an integer and comprae the month(cell value) to the lastmonth

I would however attach a sample file with dummy data, makes it easier to check and explain

 
Posted : 12/05/2023 2:03 am
Share: