VBA Autofilter

Chris4564

New Member
Joined
Mar 13, 2019
Messages
3
Hi - hoping someone can help me, I have a macro which is meant to copy a set of data into an email for distribution, part of the macro applies a filter to a column containing completion dates. After I run the macro no data is shown on the screen but when you check the filter custom filter it has been applied, frustratingly if you click OK the data that should be shown appears! Below is a simplified version of the code which still causes the issue:

Code:
Sub Test()

Dim WeekC As Date
WeekC = "20/01/2019"
Sheets("a. Quote Progress").Range("B5:AC5").AutoFilter Field:=15, Criteria1:=">" & WeekC - 1, Criteria2:="<" & WeekC + 7
End Sub

This is now driving me crazy!
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,728
Office Version
  1. 365
Platform
  1. Windows
deleted by Yongle
 
Last edited:

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,420
Office Version
  1. 2013
Platform
  1. Windows
You have Field:=15. Since your filter range starts in column B, that would put the key range as column P. Is that the correct column to filter on?
 

Chris4564

New Member
Joined
Mar 13, 2019
Messages
3
You have Field:=15. Since your filter range starts in column B, that would put the key range as column P. Is that the correct column to filter on?

Yes, if you check the filter after running the macro it's set and on the right column, there's just no data visable!
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,728
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Code:
Sub TestFilter()
   Dim WeekC As Date, Crit As [COLOR=#ff0000]Long[/COLOR]

   WeekC = [COLOR=#ff0000]DateSerial(2019, 1, 20)[/COLOR]
  [COLOR=#ff0000] Crit[/COLOR] = WeekC
   Sheets("a. Quote Progress").Range("B5:AC5").AutoFilter Field:=15, Criteria1:=">" & [COLOR=#ff0000]crit - 1[/COLOR], [COLOR=#008080]Operator:=xlAnd, [/COLOR]Criteria2:="<" & [COLOR=#ff0000]crit + 7[/COLOR]
End Sub

Date filters are frustrating for me because Excel speaks American!
The above method ensures that the criteria is a date that Excel is guaranteed to interpret correctly
 
Last edited:

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,728
Office Version
  1. 365
Platform
  1. Windows
or with fewer steps

Code:
Sub TestFilter()
  Dim Crit As Long
  Crit = CLng(DateSerial(2019, 1, 20))
  Sheets("a. Quote Progress").Range("B5:AC5").AutoFilter Field:=15, Criteria1:=">" & Crit - 1, Operator:=xlAnd, Criteria2:="<" & Crit + 7
End Sub
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,420
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

Or even this. The # symbols make the value a date literal.
Code:
Dim WeekC As Date
WeekC = #20/01/2019#
Sheets("a. Quote Progress").Range("B5:AC5").AutoFilter Field:=15, Criteria1:=">" & WeekC - 1, Criteria2:="<" & WeekC + 7
End Sub
 

Chris4564

New Member
Joined
Mar 13, 2019
Messages
3
Thanks for the input, WeekC is actually from a data entered on a userform which changes what I can do slightly, although the below is messy it works so ill live with it for now!

Code:
WeekC = ReportWeek.WCDate.Value
Sheets("a. Quote Progress").Range("B5:AC5").AutoFilter Field:=15, Criteria1:=Array _
    (Format(WeekC, "\ dd\/mm\/yyyy\"), _
    Format(WeekC + 1, "\ dd\/mm\/yyyy\"), _
    Format(WeekC + 2, "\ dd\/mm\/yyyy\"), _
    Format(WeekC + 3, "\ dd\/mm\/yyyy\"), _
    Format(WeekC + 4, "\ dd\/mm\/yyyy\")), _
    Operator:=xlFilterValues
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,420
Office Version
  1. 2013
Platform
  1. Windows
Glad you foound a solution and thanks for the feedback,
regards, JLG
 

Watch MrExcel Video

Forum statistics

Threads
1,108,789
Messages
5,524,885
Members
409,609
Latest member
Channingz

This Week's Hot Topics

Top