ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Autofilter a Date range (https://www.excelbanter.com/excel-discussion-misc-queries/120103-autofilter-date-range.html)

Steen

Autofilter a Date range
 
Hi

When I use the below code the autofilter conditions StartDate and EndDate
dosn't work. Nothing is in the "Custom filter" if I look after running the
filter. If I remove the Clng command the dates are placed in the autofilter
but it dosn't work. If I reactivate it after this everything i ok. Any help?

Private Sub cmdOK_Click()
Dim StartDateInt, EndDateInt As Integer

Sheets("Graf Data").Visible = True
Sheets("Graf Data").Activate

Columns("A:X").Select
Selection.EntireColumn.Hidden = False
With ActiveSheet
If .FilterMode Then
.ShowAllData
End If
End With

StartDate = txtStartDate.Value
EndDate = txtEndDate.Value
MaxDate = Format(Range("C459").Value, "yyyy.mm.dd")
MinDate = Format(Range("C3").Value, "yyyy.mm.dd")

If txtStartDate.Value = "" Or txtEndDate.Value = "" Or StartDate
EndDate Then
MsgBox ("You need to giv both a Start and End Date or StartDate later
than EndDate")
txtStartDate.SetFocus
ElseIf StartDate < CDate(Range("C3").Value) Or EndDate
CDate(Range("C459").Value) Then
MsgBox ("StartDate " & StartDate & " EndDate: " & EndDate & " MinDate
" & MinDate & " MaxDate " & MaxDate)
txtStartDate.SetFocus
Else

MsgBox (StartDateInt)
Selection.AutoFilter Field:=3, Criteria1:="=" & CLng(StartDate),
Operator:=xlAnd, Criteria2:="<=" & CLng(txtEndDate.Value)
If optMonth = True Then
Selection.AutoFilter Field:=1, Criteria1:="<"
ElseIf optWeek = True Then
Selection.AutoFilter Field:=2, Criteria1:="<"
End If
End If
Sheets("Graf Data").Visible = False
Sheets("Statistics").Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub


All times are GMT +1. The time now is 12:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com