Delete Rows whose value is not between 2 dates
"Dominique Feteau" wrote:
I have a report that is imported into excel. I dont need all the data.
Column E has dates. I'd like to delete the rows that arent between 2 dates.
You can use the value property of Range's Cells property to grab the data
out of the cell in column E. Delete the row if it doesn't meet your
criteria. Here's some code to get you started. This assumes you've selected
all the relevant data before you start the macro:
numRows = Selection.Rows.Count
startDateString = InputBox("Enter a start date:", "Start Date", "MM/DD/YY")
If (IsDate(startDateString)) Then
startDate = CDate(startDateString)
For i = numRows To 1 Step -1
If Selection.Rows(i).Cells(1, 5).Value < startDate Then
Selection.Rows(i).Delete
End If
Next i
End If
IsDate checks that you entered a string in the InputBox that can be
converted to a date.
CDate actually makes a Date type out of the string. This assumes you have
real dates in column E. If your local settings are not US, you may have to
make some adjustment. I'm not sure CDate is global-friendly. But you have
to have your types right if you want the comparisons to work as expected.
Cells(1,5) is the cell in row 1 (the current row) of the Selection.Rows(i)
row, in column E.
If other people are going to use your macro, you have a lot of extra work to
make this macro more bulletproof. And if you don't want to do any work to set
things up before the macro runs, that's some extra code.
--Shawn
|