PDA

View Full Version : [SOLVED:] Wildcard Filter Not Working For Dates



framcc06
02-06-2019, 01:46 PM
Hi,

I am currently using a userform for filtering a table in a sheet based on whats entered in some text boxes


Selection.AutoFilter Field:=1, Criteria1:=UserForm1.tbFirstName.Value & "*" 'Filter by First Name
Selection.AutoFilter Field:=2, Criteria1:=UserForm1.tbLastName.Value & "*" 'Filter by Last Name
Selection.AutoFilter Field:=3, Criteria1:=UserForm1.tbDOB.Value & "*" 'Filter by Date of Birth


The First Name & Last Name filters work ok but the Date of Birth does not (assuming because the cells are formatted as dates).

Is there anything I need to add/change in order for it to work.

Thanks

Fra

Paul_Hossler
02-06-2019, 03:16 PM
Are you sure you need the wildcards?

For example (John Smith 2/6/2019)

"Jo*"
"Sm*"
"2/6/*"

doesn't seem logical for a date. How do you intend to use the date search?

Could you use a date range (From - To)?


Then the filter would work like the fragment below



Option Explicit
Sub Macro1()
Dim D1 As Date, D2 As Date

D1 = #2/1/2019#
D2 = #2/28/2019#

Range("A1").AutoFilter
ActiveSheet.Range("$A$1:$A$44").AutoFilter Field:=1, Criteria1:=">=" & Format(D1, "mm/dd/yyyy"), Operator:=xlAnd, Criteria2:="<=" & Format(D2, "mm/dd/yyyy")
End Sub

framcc06
02-07-2019, 10:10 AM
Hi Paul,

I intended to use the dob textbox for additional filtering purposes as for example there may be more than one person with the same name.

I found a way around this by adding another column formatted as text and copying the dates to it so the filter will work.

Thanks

Fra