G
Guest
I have one column of dates, say, in the range D511. My header is at D4.
In F2 I have a variable "FromDate" and in F3 a variable "ToDate". I would
like to be able to enter a 'From' and 'To' date in cells F2 & F3
respectively. Then have a VBA program grab those 2 dates and filter this
single column of dates by saying date range is greater than or equal to F2
AND less than or equal to F3.
Below is the VBA code I have attempted with no success. You help would be
appreciated.
Thanks,
John
Sub FilterDates()
Dim FromDate As Date
Dim ToDate As Date
FromDate = Range ("F2") 'Location on Worksheet where the FromDate will be
entered.
ToDate = Range ("F3") 'Location on Worksheet where the ToDate will be
entered.
FromDate = Worksheets("Sheet1").AutoFilter.Range("D511")
Range("D4").Select 'This is the header cell above the single column of
dates.
Selection.AutoFilter
With Worksheets("Sheet1")
If .AutoFilterMode Then
With .AutoFilter.Filters(1)
If .On Then FromDate = .Criteria1
End With
End If
End With
'Selection.AutoFilter
With Worksheets("Sheet1")
If .AutoFilterMode Then
With .AutoFilter.Filters(1)
If .On Then ToDate = .Criteria2
End With
End If
End With
Selection.AutoFilter field:=1, Criteria1:=">=FromDate", Operator:=xlAnd _
, Criteria2:="<=ToDate"
End Sub
In F2 I have a variable "FromDate" and in F3 a variable "ToDate". I would
like to be able to enter a 'From' and 'To' date in cells F2 & F3
respectively. Then have a VBA program grab those 2 dates and filter this
single column of dates by saying date range is greater than or equal to F2
AND less than or equal to F3.
Below is the VBA code I have attempted with no success. You help would be
appreciated.
Thanks,
John
Sub FilterDates()
Dim FromDate As Date
Dim ToDate As Date
FromDate = Range ("F2") 'Location on Worksheet where the FromDate will be
entered.
ToDate = Range ("F3") 'Location on Worksheet where the ToDate will be
entered.
FromDate = Worksheets("Sheet1").AutoFilter.Range("D511")
Range("D4").Select 'This is the header cell above the single column of
dates.
Selection.AutoFilter
With Worksheets("Sheet1")
If .AutoFilterMode Then
With .AutoFilter.Filters(1)
If .On Then FromDate = .Criteria1
End With
End If
End With
'Selection.AutoFilter
With Worksheets("Sheet1")
If .AutoFilterMode Then
With .AutoFilter.Filters(1)
If .On Then ToDate = .Criteria2
End With
End If
End With
Selection.AutoFilter field:=1, Criteria1:=">=FromDate", Operator:=xlAnd _
, Criteria2:="<=ToDate"
End Sub