PDA

View Full Version : [SOLVED:] I need VBA to delete lines related to date



Dereklong44
10-15-2023, 11:52 AM
I receive a daily excel table which has a variable number of lines.n be anything from 5 to 10000 lines.
Lines 1 to 11 are statis (always contain the same headers etc).
Line 12 onwards contain the data.
There are a list of dates in column U
I would like VBA to delete any lines from line 5 onwards whose date in col U is not tomorrow.

June7
10-15-2023, 12:08 PM
Bing search "Excel VBA delete rows".
Review https://analysistabs.com/vba/delete-rows-based-on-criteria-excel-macro-example-code/

Aussiebear
10-15-2023, 03:22 PM
maybe try this version


Sub sbDelete_Rows _Date()
Dim lRow As Long
Dim iCntr As Long
lRow = 10000
For iCntr = lRow To 12 Step -1
If Cells(iCntr, 21) = Today() Then
Rows(iCntr).Delete
End If
Next
End Sub

Paul_Hossler
10-15-2023, 05:07 PM
If you have a lot of rows, a more complicated approach would offer performance improvements




Option Explicit


Sub DeleteSomeRows()
Dim r1 As Range, r2 As Range, r As Range, r12 As Range
Dim tomm As Date

Application.ScreenUpdating = False

With ActiveSheet
Set r1 = .Cells(5, 21)
Set r2 = .Cells(.Rows.Count, 21).End(xlUp)
Set r12 = Range(r1, r2)
End With

tomm = Date + 1

For Each r In r12.Cells
With r
If IsDate(.Value) Then
If Int(.Value) <> tomm Then .Value = True
End If
End With
Next

On Error Resume Next
r12.SpecialCells(xlCellTypeConstants, xlLogical).EntireRow.Delete
On Error GoTo 0


Application.ScreenUpdating = True


End Sub