Today's Date

S

SITCFanTN

I need to convert the date to MM/DD/YYYY format from MM/DD/YY and then delete
all rows in the current worksheet that don't have today's date in column C.
The number of rows is variable each day. This is the code I was using,
however it does not work now and I'm not sure why, any help you can provide
is appreciated.

Sub OnlyTodaysDate()

' Change date to read format mm/dd/yyyy

Dim RowNum As Integer
Dim NextValue As String
Dim NextDate As Date

For RowNum = 1 To Range("C1").CurrentRegion.Rows.Count
NextValue = Range("C" & RowNum).Value
NextDate = DateSerial(2000 + Mid(NextValue, 1, 1), Mid(NextValue, 2,
2), Mid(NextValue, 4, 2))
Range("C" & RowNum).NumberFormat = "mm/dd/yyyy"
Range("C" & RowNum).Value = NextDate
Next RowNum


'Deletes rows where the value in column C is not today's date

Dim RowNdx4 As Long
Dim LastRow4 As Long

LastRow4 = Cells(Rows.Count, "C").End(xlUp).Row

For RowNdx4 = LastRow4 To 1 Step -1

If Cells(RowNdx4, "C").Value = FormatDateTime(Now, vbShortDate) =
False Then
Rows(RowNdx4).Delete
End If

Next RowNdx4

End Sub
 
B

Bernard Liengme

Am I missing something? If I want to test if a cell's value matches today's
date why do I need to reformat the cell? The sub below worked for me. The
date in C were real dates not text. Note the use of the Date function
(returns system date) rather than Now (returns date & time).

Sub OnlyTodaysDate()
'Deletes rows where the value in column C is not today's date

Dim RowNdx4 As Long
Dim LastRow4 As Long

LastRow4 = Cells(Rows.Count, "C").End(xlUp).Row

For RowNdx4 = LastRow4 To 1 Step -1
If Cells(RowNdx4, "C").Value <> Date Then
Rows(RowNdx4).Delete
End If
Next RowNdx4

End Sub

best wishes
 

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