Code deleting all rows in error

G

Guest

Here is the code I'm using in an existing macro to keep only rows with
today's date in column F, however it is deleting all rows. My date format in
the column is MM/DD/YYYY. Any help you can provide is appreciated. Thanks
Joyce

Keep only rows dated today

'Dim LastRow As Long
'Dim RowNdx As Long
' LastRow = Cells(Rows.Count, "F").End(xlUp).Row
' For RowNdx = LastRow To 1 Step -1
' If StrComp(Cells(RowNdx, "F"), "=today", vbTextCompare) <> 0 Then
' Rows(RowNdx).Delete
' End If
' Next RowNdx
 
J

Joergen Bondesen

Hi Joyce

Try this:

Option Explicit

Sub test()
Dim LastRow As Long
Dim RowNdx As Long

Application.ScreenUpdating = False

LastRow = Cells(Rows.Count, "F").End(xlUp).Row

For RowNdx = LastRow To 1 Step -1
If StrComp(Format(Cells(RowNdx, "F"), "MM/DD/YYYY"), _
Format(Now(), "MM/DD/YYYY"), vbBinaryCompare) <> _
0 Then

Rows(RowNdx).Delete
End If
Next RowNdx
End Sub
 
A

Ardus Petus

Sub Tester()
Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "F").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If Cells(RowNdx, "F").Value <> Date Then
Rows(RowNdx).Delete
End If
Next RowNdx
End Sub

HTH
 
M

Mike Fogleman

Try this:

Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "F").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If Cells(RowNdx, "F").Value <> Date Then
Rows(RowNdx).Delete
End If
Next RowNdx

Mike F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top