Autofilter between two times: Easier than it sounds

Kentetsu

Well-known Member
Joined
Jan 22, 2004
Messages
520
I'm fairly certain that this has to be a formatting issue of some kind.

When stepping through the code, it correctly identifies "TimeToSearch1" and "TimeToSearch2" as the times to filter for.

Code:
With Worksheets("PII Yields")
        If .FilterMode Then .Range("A2:F2").AutoFilter
        .Range("A2:F2").AutoFilter Field:=1, _
                    Criteria1:=">=" & TimeToSearch1, _
                    Operator:=xlAnd, _
                    Criteria2:="<=" & TimeToSearch2
End With

I have formatted the Target Times to match the formatting on the spreadsheet I'm trying to manipulate but it doesn't seem to make any difference. The Target Times show as time only, while the spreadsheet shows a date/time combination. So, I'm guessing that I need to somehow change the formatting at one end or the other so that they finally match. Just not sure how to get there.

There are a couple of steps before this filter process which may have something to do with the issue:

1. Filter spreadsheet by selected date.
2. Copy results and paste to new sheet as Values.
3. Format Column A (Dates) to .Range("A:A").NumberFormat = "HH:MM;@"

Thanks for your time...
 
Thanks again. I think you've given me enough to get it working. Unfortunately, just prior to calling it a day last night Excel crashed on me and this morning I've come to find that the "recovery" option must have failed. So, I'm starting from scratch once again. Oh well, practice makes perfect I guess. :)
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Okay, I tried the following and even though the formatting looks correct it returns everything rather than filter it.

Code:
w.Cells.AutoFilter Field:=1, Criteria1:=">=" & DateToSearch & " " & Time1, _
Operator:=xlOr, Criteria2:="<=" & DateToSearch & " " & Time2
 
Upvote 0
The problem is that I turned the info into a table, so now has to be handled a bit differently.

This works for sorting the time span (already sorted for date prior to turning it into a table):

Code:
ActiveSheet.ListObjects("TableLoaded").Range.AutoFilter Field:=7, Criteria1 _
                :=">=" & Time1, Operator:=xlAnd, Criteria2:="<=" & Time2

Thanks for the assistance...
 
Upvote 0

Forum statistics

Threads
1,215,886
Messages
6,127,575
Members
449,385
Latest member
KMGLarson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top