Forum Discussion
joeycc
Aug 17, 2022Copper Contributor
Macro/VBA to move + delete entries based on date
Hey! I haven't been able to figure this out for about a week now. I have a sheet for work that is used to check if clients have visited within the past two weeks. I would like to move rows over 1...
- Aug 20, 2022
Sub fourteendays() Dim ws As Worksheet Dim ws1 As Worksheet Dim lastrow As Long Dim i As Long Dim k As Long Dim todaydate As Date todaydate = Date Set ws = Worksheets("Tabelle1") Set ws1 = Worksheets("Tabelle2") k = ws1.Cells(Rows.Count, 1).End(xlUp).Row lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row For i = 2 To lastrow If Cells(i, 1).Value <> "" Then If Cells(i, 1).Value < todaydate - 14 Then ws.Range("A" & i, "I" & i).Copy Destination:=ws1.Range("A" & k + 1) k = k + 1 ws.Rows(i).Delete i = i - 1 Else End If Else End If Next i Application.CutCopyMode = False End Sub
Maybe with these lines of code. In the attached file you can click the button in cell N2 to run the macro. The dates are in column A of sheet "Tabelle1" and in column B to K are random numbers for this example.
HansVogelaar
Aug 17, 2022MVP
I'd leave all data on the one sheet, and use a filter on the date column to show or hide rows as needed.
- joeyccAug 17, 2022Copper ContributorI don't think this would work in my situation because I have a function that uses the entries on the sheet to check whether a client has come in the last few weeks. So, I need the one main sheet to only have two weeks worth of entries