I am having probelms with excel not returning the correct date format to the autofilter. User inputs date, date is inserted into a cell and this is used in the autofilter to filter dates. Cells are formated to DD/MMM/YY. When the user inputs the date it is copied correctly to the cell reference but the autofilter does not recognise it correctly.
Any and all help would be greatly appreciated.
Any and all help would be greatly appreciated.
Code:
Sub test_filter_dates()
Dim r As Range, filt As Range, d1 As Long, d2 As Long
With Worksheets("Sheet1")
Dim vResponse As Variant
Dim vResponse1 As Variant
Do
vResponse = Application.InputBox( _
Prompt:="Enter start date Format MM/DD/YY:", _
Title:="Start Date", _
Default:=Format(Date, "DD/MMM/YY"), _
Type:=2)
If vResponse = False Then Exit Sub 'User cancelled
Loop Until IsDate(vResponse)
Range("F1").Value = CDate(vResponse)
Do
vResponse1 = Application.InputBox( _
Prompt:="Enter end date Format DD/MMM/YY:", _
Title:="End Date", _
Default:=Format(Date, "DD/MMM/YY"), _
Type:=2)
If vResponse1 = False Then Exit Sub 'User cancelled
Loop Until IsDate(vResponse1)
Range("G1").Value = CDate(vResponse1)
d1 = .Range("F1").Value
d2 = .Range("G1").Value
.Range("A1").CurrentRegion.AutoFilter Field:=.Range("A1").Column, Criteria1:=">=" & CDate(d1) _
, Operator:=xlAnd, Criteria2:="<=" & CDate(d2)
Set filt = .Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible)
'filt.Copy
With Worksheets("Sheet2")
.Cells.Clear
filt.Copy
.Range("a1").PasteSpecial
.Range("A1:B1").EntireColumn.AutoFit
End With
.Range("A1").CurrentRegion.AutoFilter
End With
Worksheets("Sheet1").Activate
End Sub