0% found this document useful (0 votes)
222 views

Autofilter With Column Formatted As Date: 10 Answers

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

Autofilter With Column Formatted As Date: 10 Answers

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1

Products Search… 1 1

Home
Autofilter with column formatted as date Ask Question

PUBLIC Asked 7 years, 5 months ago Active 1 year, 1 month ago Viewed 62k times

Questions
I am using an AutoFilter with VBA in Excel that works for regular filters, but not a column formatted
Tags The Overflow Blog
as date.
Users 13 How often do people actually copy and
I can filter it manually. If I run my code, it filters nothing but when I check the filter and then only paste from Stack Overflow? Now we
FIND A JOB click ok (no change being applied to the filter criteria), it filters correctly. know.
Jobs
Podcast 331: One in four visitors to Stack
Here is my code:
Companies 3 Overflow copies code

TEAMS ws.ListObjects(SheetName).Range.AutoFilter Field:=3, Criteria1 _ Featured on Meta


:=">" & CDate([datecell]), Operator:=xlAnd, Criteria2:= _
"<=" & CDate(WorksheetFunction.EoMonth([datecell], 3))
Stack Overflow for Stack Overflow for Teams is now free for
Teams – Collaborate up to 50 users, forever
and share knowledge
with a private group.
It seems to be a common problem, but I have not found a solution. New onboarding for review queues

When I run a recorded macro, it does not work either. Should the [complete] tag be removed?

Outdated Answers: results from use-case


excel vba date autofilter survey

Downvotes Survey results


Share Edit Follow edited Feb 18 '20 at 16:42 asked Nov 4 '13 at 11:44
Create a free Team Community ♦ Spurious Hot Meta Posts
1 1 1,705 2 20 45
What is Teams?
13 Why was this answer to “numpy-like-
package-for-node” deleted?
If you change your date cells format to general does it show a number? – Sam Nov 4 '13 at 12:31

Yes, as I said, everything works as it should. There is a problem when the filter is applied. Because the
54 people chatting
recorded macro does not work either. – Spurious Nov 4 '13 at 12:40

Try converting to text instead. such as CDate([datecell]) change to Format([datecell],


"dd-MMM-yy") – Sam Nov 4 '13 at 12:47 Linked
Does not work either, see the answer and my comment below. It's really absurd. – Spurious Nov 4 '13 at 0 Issues with date filtering in VBA
12:54
-1 filter based on current date using macros
That does seem a bit odd yes.... what value is [datecell] and
WorksheetFunction.EoMonth([datecell], 3) if you Debug.Print or msgbox them? – greaterthan and lessthan
Sam Nov 4 '13 at 12:56
2 VBA code to filter sheet by exact date using
[datecell] = 31.12.2012 WorksheetFunction.EoMonth([datecell], 3) = 41364 We might be onto something. InputBox
Although it's formatted as date in the sheet. – Spurious Nov 4 '13 at 13:01
2 Filtering Data for 2 months prior
Just changed the format for the date from Date to General and it didnt change anything. – Spurious Nov
4 '13 at 13:04 0 Get Date Autofilter in Excel VBA

if you msgbox(Format([datecell],"dd-MMM-yy")) does it give you 31-Dec-2012? Changing the 0 Visual basic excel: filter to show dates
cell format won't do much, as the underlying value will stay the same. – Sam Nov 4 '13 at 13:08 earlier than today

Note my comment after my answer. The "/" in the format command gets translated to the system date 0 Taking over the date correctly to select the
separator. To force slashes as a separator, you need to do something like: "m""/""d""/""yyyy", or searching criteria
"m\/d\/yyyy" I keep forgetting the subtle differences between VBA Format and Excel TEXT functions (and
the .NumberFormat property) – Ron Rosenfeld Nov 4 '13 at 18:48 1 Autofilter messing up date format?

Add a comment 0 VBA - Passing DateVariable to Autofilter


fails

10 Answers Active Oldest Votes 0 VBA Autofilter exclude Date criteria

See more linked questions


Dates can be tricky with Excel VBA AutoFilter. Some find it easier to just loop through the array to
be filtered. Related
19
Sometimes I have found that one can use the numeric value of the date, especially when dealing 1901 How to return only the Date from a SQL
with "dates between" Server DateTime datatype

2214 Compare two dates with JavaScript


Criteria1:= ">" & CDbl([datecell])
Criteria2:= "<=" & CDbl(WorksheetFunction.EoMonth([datecell], 3)) 1436 Where can I find documentation on
formatting a date in JavaScript?

1686 Detecting an “invalid date” Date instance in


Note that the above need to be "real dates" and not strings that look like dates. Even a single
JavaScript
"string date" will mess things up.
1206 YYYY-MM-DD format date in shell script

Share Edit Follow edited Nov 4 '13 at 13:21 answered Nov 4 '13 at 12:43 2547 How do I get the current date in
Ron Rosenfeld JavaScript?
39.6k 6 22 47
2623 How to format a JavaScript date

Yeah, tried that as well earlier and it didnt work either. It's a mystery to me. Especially given the fact that 1 Excel VBA Saving autofilter settings with
the recorded macro does not work either. – Spurious Nov 4 '13 at 12:48 dates
1 Another option, when dealing with "dates between", is to use the Excel stored number. See my Edit in the 0 Filter Criteria1 and Criteria 2 undefined on
answer – Ron Rosenfeld Nov 4 '13 at 12:59 column of Dates
Something else to check is that all of your dates are really dates, and not strings that happen to look like
dates. Even a single "string date" in the list will mess things up. – Ron Rosenfeld Nov 4 '13 at 13:03 Hot Network Questions
Edit your answer to only show the CDbl Version and I mark it as correct and vote it up. – Spurious Nov 4
How to provide an implementation of memcpy
'13 at 13:06
Someone asked me to login to his bank account
Looking at some of your comments, and noting that your settings have the dot as a date separator, led me and make transfers to another account
to do some testing. It seems that if the dot is set as your system date separator, even if you enter
Format([date],"m/d/yyyy") the system will still change the separator to a dot. In order to get the proper US Difference in weight from Cura to printed model
format with a slash separator, you need to use a format code of: "m""/""d""/""yyyy" Try this latter method
and see how it works on your system. This behavior is outlined in HELP for the VBA Format function. – Disabling Access to Google Chrome Saved
Passwords in a Secure Way
Ron Rosenfeld Nov 4 '13 at 18:41
Is there any way to use microsoft office in ubuntu
Show 5 more comments without using wine? or any other alternative similar
to microsoft office which uses the same format

Income tax rate calculator in c++


Autofilter() works with 'universal' format yyyy-mm-dd , i.e.:
Does a character taking the Ready action perceive
time?
10 Criteria1:= ">" & Format([MY_DATE],"yyyy-mm-dd")
What would prevent growing minions as cannon
Criteria2:= "<=" & Format([MY_DATE],"yyyy-mm-dd") fodder from being a viable alternative to hiring
henchmen?

How to deal with professors who don't follow the


It's better because Excel can't 'understand' it wrong . If you use mm/dd/yyyy or dd/mm/yyyy Excel
text
can fit 02/jan as 01/feb.
Why do people at the top of a
bureaucracy/government have to get paid the
see: http://www.oaltd.co.uk/ExcelProgRef/Ch22/ProgRefCh22.htm most?

What are the advantages of using DC electric


power over AC, as e.g. done in the EMB 135/145?
The Rules for Working with Excel (International Issue)
Why does trying to break into the NT 3.1 kernel
reboot my 486DX4 machine?
2. When you have to convert numbers and dates to strings for passing to Excel (such as
in criteria for AutoFilter or .Formula strings), always explicitly convert the data to a US- What is a common idiom meaning, 'that a situation
is likely to change all of a sudden without notice or
formatted string, using Trim(Str(MyNumber)), or the sNumToUS() function shown reason'?
earlier, for all number and date types. Excel will then use it correctly and convert it to
Why does Joe Biden always tweet from @POTUS
the local number/date formats. and not @JoeBiden?

Are All the Items the Same?

Is OPTION (RECOMPILE) used in production


Edit: environments?

Why use で in「私でよければ⼊部させてくださ


We can create an universal Function using Application.International like: い」?

Why is the market for cryptographic primitives


Sub MySub() non-commercialized?
Select Case application.International(xlDateOrder) Having a string representing items in a cart,
Case Is = 0 elaborate taxes and totals. Interview task in
dtFormat = "mm/dd/yyyy" Python
Case Is = 1
Multidimensional utility
dtFormat = "dd/mm/yyyy"
Case Is = 2 How do I make my soup a more appealing colour?
dtFormat = "yyyy/mm/dd"
Case Else Why do news articles often refer to the leader as
opposed to the country?
dtFormat = "Error"
End Select Is offering jobs to students I'm teaching as a
"representative of industry" OK?
Debug.Print (dtFormat)
Is there a deleted scene with a Jyn and Cassian
kiss?
...
Criteria1:= ">" & Format([MY_DATE],dtFormat)
Question feed
Criteria2:= "<=" & Format([MY_DATE],dtFormat)
...

End Sub

Share Edit Follow edited Sep 21 '15 at 16:21 answered Aug 6 '15 at 21:13
Makah
3,960 3 40 61

Add a comment

you need to convert the format to the american format, like: ">" & Format([datecell], "mm/dd/yyyy")
VBA does not understand another format.
4
Share Edit Follow answered May 22 '14 at 15:12
Karlos Henrique
41 1

Add a comment

This syntax works for me:

1 .AutoFilter Field:=2, Operator:=xlFilterValues, Criteria2:=Array(2, Format(Now, "yyyy-mm-dd"))

Hint obtained through a macro registration

Share Edit Follow answered Apr 29 '16 at 14:35


Mago Fabian
11 1

Add a comment

Match your "dd-mm-yyy" to the format of the column, so if you have "16-Aug-16" as your source
data formatting then make the filter as "dd-mmm-yy"
0
Share Edit Follow answered Aug 16 '16 at 8:28
Graham P
11

This worked for me... my column was formatted as dd-mmmm-yyyy and that threw everything out.
Changing the criteria fixed the issue. – SlowLearner Sep 23 '16 at 12:12

Add a comment

One solution is to set up correctly the format of the target column. If its formatted as "custom", even
if it looks like a date, it won't work.
0
You are asking VBA to filter based on a date, therefore the format of the columns has to be a date
too.

Share Edit Follow answered Jan 29 '19 at 14:24


Andrés Roldán
1

Add a comment

SO this worked for me pretty clean

0 ActiveSheet.Range("$A$1:$K$35727").AutoFilter Field:=1, Criteria1:= _


">=" & Range("G1"), Operator:=xlAnd, Criteria2:="<=" & Range("H1")

You can try this as well

Expected output will start date in my G1 cell and end date will be H1 cell.

Share Edit Follow edited Nov 22 '19 at 19:40 answered Nov 22 '19 at 17:55
aviboy2006 Sudhanshu Soni
5,673 4 17 35 1 2

Add a comment

Karlos Henrique, Thanks for suggesting Format([datecell], "mm/dd/yyyy"). It works in my file. My


previous code is :
0
Range("A1:D" & Trim(Str(LstRec))).AutoFilter Field:=2, _
Criteria1:=">=" & StrtDt, _
Operator:=xlAnd, _
Criteria2:="<=" & EndDt

My modified code is :

Range("A1:D" & Trim(Str(LstRec))).AutoFilter Field:=2, _


Criteria1:=">=" & Format(StrtDt, "mm/dd/yyyy"), _
Operator:=xlAnd, _
Criteria2:="<=" & Format(EndDt, "mm/dd/yyyy")

Thanks.

Share Edit Follow answered Jun 14 '14 at 5:59


pmyk
1 1

Add a comment

I had similar problem, while filtering column with date. When I changed system date format to
01/01/2020, it converted my date to 1/1/2020 in Criteria field. My solution was to convert arguments
0 to str during setting to function.

Share Edit Follow answered Feb 27 '20 at 12:37


Piotr Kotarba
25 5

Add a comment

here's the occam's razor solution... try putting this in Autoopen for the spreadsheet or if you need
to, modify it for the sheet that you wish to affect. it will cause the drop down filters for the date
-1 headers to appear as individual dates and not as a date hierarchy.

ActiveWindow.AutoFilterDateGrouping = False

Share Edit Follow edited Nov 14 '19 at 17:20 answered Nov 14 '19 at 17:14
Joe Messingschlager
61 10

Add a comment

Your Answer

Links Images Styling/Headers Lists Blockquotes Code HTML Tables Advanced help

Post Your Answer

Not the answer you're looking for? Browse other questions tagged excel vba date autofilter or ask
your own question.

STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn Instagram
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Terms of Service
Disable Responsiveness Contact Us
Cookie Settings
site design / logo © 2021 Stack Exchange Inc; user contributions
Cookie Policy licensed under cc by-sa. rev 2021.4.20.39115

You might also like