Hi Seeker,
Sorry it has taken so long to get back to you but I have had a lot of
problems with the date code. I am in a d/m/y date format region and everytime
I set the Autofilter with the code it would not display until I clicked the
drop down and closed it manually. I believe you also had that problem. I
finally found the following post by Dave Petersen on 20th Feb 2009.
Start of Dave Petersen’s Post ********************************************
This is from "Excel 2002 VBA Programmer's Reference"
Written by John Green, Stephen Bullen, Rob Bovey and Robert Rosenberg
http://www.oaltd.co.uk:80/ExcelProgRef/Ch22/ProgRefCh22.htm
Search for "Range.AutoFilter" and you'll see this note:
Range.AutoFilter
The AutoFilter method of a Range object is a very curious beast. We are
forced
to pass it strings for its filter criteria and hence must be aware of its
string
handling behaviour. The criteria string consists of an operator (=, >, <, >=
etc.) followed by a value.
If no operator is specified, the "=" operator is assumed. The key issue is
that
when using the "=" operator, AutoFilter performs a textual match, while using
any other operator results in a match by value. This gives us problems when
trying to locate exact matches for dates and numbers.
If we use "=", Excel matches on the text that is displayed in the cell, i.e.
the
formatted number. As the text displayed in a cell will change with different
regional settings and Windows language version, it is impossible for us to
create a criteria string that will locate an exact match in all locales.
There is a workaround for this problem. When using any of the other filter
criteria, Excel plays by the rules and interprets the criteria string
according
to US formats. Hence, a search criterion of ">=02/01/2001" will find all
dates
on or after 1st Feb, 2001, in all locales.
We can use this to match an exact date by using two AutoFilter criteria. The
following code will give an exact match on 1st Feb, 2001 and will work in any
locale:
Range("A1

200").AutoFilter 2, ">=02/01/2001", xlAnd, "<=02/01/2001"
So in your case:
ActiveSheet.Range("$A$1:$S$277").AutoFilter Field:=1, _
Criteria1:=">=" & format(StartDate,"mm/dd/yyyy"), _
Operator:=xlAnd, Criteria2:="<=" & format(EndDate,"mm/dd/yyyy")
End of Dave Petersen’s post *******************************************
Anyway I have come up with the following code. If not correct then confirm
that all the following are correct.
My understanding is that all of the dropdowns have an All option. Therefore
the code sets the filter to All if that is selected. Code is a bit more
complex for the dates in this area but I think I have it correct.
Your dates have both date and time displayed. (If not then modify the
formats in the Criteria setting but ensure you use the American date format
irrespective of your regional date format)
Your dates in the AutoFiltered list, DropDown list and the DropDown cell
should all be formatted the same.
If still not working and you would like me to have a look at your workbook
then if you want to get a hotmail (or some other email provider) address that
you can abandon later if you want and post it then I will reply and you may
send me the workbook.
When posting an email address don’t just type out the address in the post.
Post it something like myname at hotmail dot com and then say replace myname
with (whatever name you use for the address.) This helps to prevent automated
programs searching the internet to find email addresses in the text because
it does not look and email address.
Sub ProcessPrintReport()
Dim crit1 As Variant 'Holds criteria cell values
Dim crit2 As Variant
Dim crit3 As Variant
Dim crit4 As Variant
Dim crit5 As Variant
Dim crit6 As Variant
'Test that all criteria cells have been populated.
'Assign the criteria cell values to variables.
'Edit the criteria cell addresses to suit your project.
'Can leave crit1 to crit5 variables as is.
With Sheets("Oda Input")
If .Range("J19") = "" Then
GoTo MsgeToUser
Else
crit1 = Format(.Range("J19"), "000000")
End If
If .Range("J20") = "" Then
GoTo MsgeToUser
Else
crit2 = .Range("J20")
End If
If .Range("J22") = "" Then
GoTo MsgeToUser
Else
crit3 = .Range("J22")
End If
If .Range("J35") = "" Then
GoTo MsgeToUser
Else
crit4 = .Range("J35")
End If
If .Range("J36") = "" Then
GoTo MsgeToUser
Else
crit5 = .Range("J36")
End If
If .Range("J38") = "" Then
GoTo MsgeToUser
Else
crit6 = .Range("J38")
End If
End With
'If no blank criteria then skip the error message.
GoTo PastErrorMsge
MsgeToUser:
MsgBox "All criteria cells must be populated." _
& vbCrLf & "Processing terminated."
Exit Sub
PastErrorMsge:
With Sheets("Records")
'Ensure that AutoFilter is turned on.
'This avoids error problems if not turned on.
.Range("A1").AutoFilter Field:=1
'Remove all existing filters (if any).
If .FilterMode Then
.ShowAllData
End If
End With
With Sheets("Records").AutoFilter.Range
'Counting from left of AutoFiltered range
'edit the Field number to suit your fields
'no need to change crit1 to crit5 variables.
If crit1 = "All" Then
.AutoFilter Field:=5
Else
.AutoFilter Field:=5, Criteria1:="=" & crit1
End If
If crit2 = "All" Then
.AutoFilter Field:=2
Else
.AutoFilter Field:=2, Criteria1:="=" & crit2
End If
If crit3 = "All" Then
.AutoFilter Field:=21
Else
.AutoFilter Field:=21, Criteria1:="=" & crit3
End If
If crit4 = "All" Then
.AutoFilter Field:=3
Else
.AutoFilter Field:=3, Criteria1:="=" & crit4
End If
If crit5 = "All" Then
If crit6 = "All" Then
.AutoFilter Field:=16 'Both All - No filter
Else
'Filter on crit6 only
.AutoFilter Field:=16, Criteria1:="<=" _
& Format(crit6, "mm/dd/yyyy hh:mm")
End If
Else
'crit5 has date
If crit6 = "All" Then 'Filter on crit5 only
.AutoFilter Field:=16, Criteria1:=">=" _
& Format(crit5, "mm/dd/yyyy hh:mm")
Else
'Both crit5 & crit6 have dates
.AutoFilter Field:=16, Criteria1:=">=" & _
Format(crit5, "mm/dd/yyyy hh:mm"), Operator:= _
xlAnd, Criteria2:="<=" & _
Format(crit6, "mm/dd/yyyy hh:mm")
End If
End If
End With
'Clear any existing data from the Print Sheet
Sheets("Print").Cells.Clear
'Copy the Filtered data to Print Worksheet
Sheets("Records").AutoFilter.Range.Copy _
Destination:=Sheets("Print").Range("A1")
'Following is optional code.
'it makes headers bold, Autofits columns
'and then selects the Print worksheet.
With Sheets("Print")
.Range(.Cells(1, 1), _
.Cells(1, Columns.Count) _
.End(xlToLeft)).Font.Bold = True
.UsedRange.Columns.AutoFit
.Select
End With
'Ensure cell A1 is top left cell of window
'so that data is visible.
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
End Sub