Home > Software engineering >  Creating a Simple VBA Filter
Creating a Simple VBA Filter

Time:12-01

I have been trying to write what should be a fairly simple piece of code to dynamically filter dates in a table based on the current date. I've searched online quite a bit and have gotten conflicting information and have not been able to get anything to work. Specifically, I've been getting error 438 with the below code (and variations).

I am brand new to VBA and am self-teaching so apologize for any basic errors.

ActiveSheet.ListObjects.ListColumns(5).Range.AutoFilter Field:=5,Criteria1:=">=" & Date

I greatly appreciate any assistance.

CodePudding user response:

AutFilter Date Column

  • The criteria needs to be a string:
Option Explicit

Sub FilterByToday()
    Dim Crit1 As String: Crit1 = ">=" & Format(Date, "mm/dd/yyyy")
    ActiveSheet.ListObjects("Table1").Range.AutoFilter 5, Crit1
End Sub
  • This doesn't work for me, possibly because my date separator is a dot unless I change Crit1 to:

    Crit1 = ">=" & Format(Date, "mm\/dd\/yyyy") ' note the backslashes
    
  • Related