Sub DateRange()
Dim d As Double
Dim i As Variant
Dim cel As Range, celStart As Range, celEnd As Range, rg As Range, del As Range
Dim StartDate As Long, EndDate As Long
StartDate = CLng(CDate(Application.InputBox("Please enter starting date", Type:=2)))
If StartDate = 0 Then Exit Sub
EndDate = CLng(CDate(Application.InputBox("Please enter ending date", Type:=2)))
If EndDate = 0 Then Exit Sub
Range("J2").Select
'try using the date serial
Range("J2").FormulaR1C1 = "=DATESERIAL(MID(J2,7,4),LEFT(J2,2),MID(J2,4,2))"
'
Set rg = Range("J2") 'First date cell
Set rg = Range(rg, Cells(Rows.Count, rg.Column).End(xlUp)) 'All the dates in that column
If EndDate < StartDate Then
d = EndDate
EndDate = StartDate
StartDate = d
End If
Application.ScreenUpdating = False
Set rng = Intersect(Range("J:J"), ActiveSheet.UsedRange)
For Each cell In rng
cell.Activate
If (cell.Value) < StartDate Or (cell.Value) > EndDate Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete
Application.ScreenUpdating = True
End Sub
|