Help on my filter please

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Please could you help as to why the filter will not work in this code. Need yesterdays & todays filtered.


VBA Code:
Sub FindValues()

    Dim ws           As Worksheet
    Dim Rng          As Range
    Dim Lrow         As Long, Yesterday As Long, ToDay As Long
    Dim Comparerng   As Variant, x As Variant, y As Variant, i As Variant
    Dim Sourcerng    As Range
    Dim YDate        As Date
    Dim TDate        As Date
    
    On Error Resume Next
    Set ws = ActiveSheet
    Lrow = ws.Range("A2").End(xlDown).Row
    Set Rng = ws.Range("A2:J" & Lrow)
    
        ToDay = (DateSerial(Year(Date), Month(Date), Day(Date)))
        TDate = ToDay
        TDate = Format(TDate, "mm/dd/yyyy")
        
        Yesterday = (DateSerial(Year(Date), Month(Date), Day(Date - 1)))
        YDate = Yesterday
        YDate = Format(YDate, "mm/dd/yyyy")
        
        Rng.AutoFilter Field:=1, _
        Criteria1:=">" & YDate, _
        Operator:=xlAnd, _
        Criteria2:="<" & TDate

       End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
It could be possible you need >= and <=

Perhaps run the custom filter first using the macro recorder to see how the dates look.

Normally you just need to use date for example

VBA Code:
    today = Date
    yesterday = today - 1


    ActiveSheet.Range("$A$1:$A$30").AutoFilter Field:=1, Criteria1:= _
                                               ">=" & yesterday, Operator:=xlAnd, Criteria2:="<=" & today
 
Upvote 0
I`ve tried what you said but the criteria`s says expression not defined in context??
 
Upvote 0
Please try the following

VBA Code:
Sub Filter_2_days()
    With ActiveSheet.Range("A2", Cells(Rows.Count, "A").End(xlUp))
        .AutoFilter 1, Format(Date, "mm/dd/yyyy"), 2, Format(Date - 1, "mm/dd/yyyy")
    End With
End Sub
 
Upvote 0
Please try the following

VBA Code:
Sub Filter_2_days()
    With ActiveSheet.Range("A2", Cells(Rows.Count, "A").End(xlUp))
        .AutoFilter 1, Format(Date, "mm/dd/yyyy"), 2, Format(Date - 1, "mm/dd/yyyy")
    End With
End Sub
I tried this and it just filters the column blank??
The same happened on my previous attempts.
 
Upvote 0
1655459428990.png
1655459428990.png
 
Upvote 0
Thanks Eric. Try this now: (EDITED)

VBA Code:
Sub Filter_2_days_v2()
    With ActiveSheet.Range("A1", Cells(Rows.Count, "A").End(xlUp))
        .AutoFilter 1, Format(Date, "dd/mm/yyyy"), 2, Format(Date - 1, "dd/mm/yyyy"), 0
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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