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.
OliverScheurich
Aug 20, 2022Gold Contributor
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.
- AlexisSanchez2Aug 29, 2024Copper Contributor
OliverScheurich This is great. Is there a way to do this exact thing except have the final destination be a worksheet in another workbook instead?
- HansVogelaarAug 29, 2024MVP
Change the line
Set ws1 = Worksheets("Tabelle2")
to
Set ws1 = Workbooks("OtherWorkbook.xlsx").Worksheets("OthwerWorksheet")
substituting the correct names, of course.
- joeyccAug 20, 2022Copper Contributorworked perfectly once I converted the dates back to US format, thanks a ton !