Vasant Patil
Vasant Patil

Reputation: 11

Filter data in VBA with greater than particular date

I am trying to filter data with date greater than given date however it is not picking complete list of date.

Here is my code

Sub Vas()

    Startdate="13-10-2020"
    Selection.autofilter filed:=6,creteria1:=">=" & startdate

End sub

Above formula is working when i am trying to filter with equal("=") but failed when i use greater than. Can anyone please help!!

Upvotes: 1

Views: 2695

Answers (3)

MikeR
MikeR

Reputation: 71

One better way to do that is to specify your date as numeric (Long), and use the DateSerial function, like that :

Sub Vas()
Dim StartDate As Long
    StartDate = DateSerial(2020, 10, 13)
    Selection.AutoFilter Field:=6, Criteria1:=">=" & StartDate
End Sub

Upvotes: 2

Gustav
Gustav

Reputation: 55806

Dates are DateTime, not Text, thus:

Sub Vas()

    Dim StartDate as Date

    StartDate = #10/13/2020#
    Selection.Autofilter Field:=6,Criteria1:=">=" & StartDate

    ' or perhaps, as your default date format is dd-mm-yyyy:
    Selection.Autofilter Field:=6,Criteria1:=">=#" & Format(StartDate, "yyyy\/mm\/dd") & "#"

End sub

Upvotes: 1

MD Ismail Hosen
MD Ismail Hosen

Reputation: 118

Use this

Sub Vas()

Startdate="13-10-2020"
Selection.autofilter filed:=6,creteria1:=">" & startdate

End sub

Upvotes: 0

Related Questions