Consulting

Results 1 to 3 of 3

Thread: Wildcard Filter Not Working For Dates

  1. #1
    VBAX Regular
    Joined
    Sep 2017
    Posts
    25
    Location

    Wildcard Filter Not Working For Dates

    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

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Sep 2017
    Posts
    25
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •