Userform - Date Autofilter

Takes2ToTango

Board Regular
Joined
May 23, 2023
Messages
69
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a Userform that autofilters a table using wildcards and user input.

I have attempted to replicate this code for filtering the table by 'years' from dates, however all I am getting is a blank table.

1729516531821.png

Using the above userform I put in '2024'

VBA Code:
Sheet4.ListObjects("Table").Range.AutoFilter Field:=8, Criteria1:="*" & "/" & "*" & "/" & DateTxt.Value

This code then runs and i was hoping that the table would then be filtered by all dates ending in '2024'

However I only get an empty table and no results
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Your criteria is a string. AFAIK, you must use the Like operator when comparing string with wild cards. I'd try = Like "*" ... first.
If that didn't work I try using a full valid date as criteria. If your data is text data type that would be in US format as in "10/21/2024". If the data is date data type then #10/21/2024#. You could also just look for the year and forget all the wild card stuff for the rest of it. Something like Criteria1:= Year(DateTxt) = 2024 for actual dates, or perhaps ="2024" for strings.
 
Upvote 0
Thanks for your reply.

I have had a go with changing the 'Like' ** and changing the criteria to remove the wildcard but no luck. We may use multiple years so it'll have to be able to look at 2022 etc for example
 
Upvote 0
Hi,
if you are wanting to filter all dates for a year specified in your Textbox - you could try creating date range using dateserial function and pass to long variables for the Criteria.

VBA Code:
    Dim FilterYear     As Long
    Dim StartDate      As Long, EndDate As Long
 
    FilterYear = Val(DateTxt.Value)
 
    StartDate = DateSerial(FilterYear, 1, 1)
    EndDate = DateSerial(FilterYear, 12, 31)
 
    Sheet4.ListObjects("Table").Range.AutoFilter Field:=8, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate

This assumes that the dates in your table are real dates.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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