VBA Autofilter Not Working with Date

JNM

New Member
Joined
Jan 24, 2012
Messages
35
Good afternoon,

I am getting error 1004 "AutoFilter method of Range class failed" when opening a workbook. I would like the workbook to autofilter the Start Date to those dates from the past two weeks upon opening.

I would appreciate any help or suggestions...


The workbook has one worksheet in it that has "on change" code that autopopulates the date and time fields. Here is that code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    
    Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    
    With Sheet1
        With .Range("A6 :A" & LastRow)
            .AutoFilter
             .AutoFilter Field:=1, Criteria1:=Range("A1").Value
        End With
    End With
  
    If Not Application.Intersect(Target, Columns("D:D")) Is Nothing Then
        Target.Offset(0, 1).Value = Format(Now, "mm/dd/yy")
        Target.Offset(0, 2).Value = Format(Now, "hh:mm AM/PM")
    End If
    
    If Not Application.Intersect(Target, Columns("G:G")) Is Nothing Then
        Target.Offset(0, 1).Value = Format(Now, "mm/dd/yy")
        Target.Offset(0, 2).Value = Format(Now, "hh:mm AM/PM")
    End If
  
End Sub

Here is the "FilterDate Code"

Code:
Sub FilterDate()
Dim Date1 As Date
Date1 = Date - 14
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
    
With Sheet1
           .AutoFilterMode = False
        With .Range("A6 :A" & LastRow)
            .AutoFilter
                .AutoFilter Field:=5, Criteria1:=">=" & Date1, visibledropdown:=False
            End With
End With
    
End Sub

Here is the "Open Workbook" code:
Code:
Private Sub Workbook_Open()
FilterDate
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,
see if this update to your FilterDate code helps:

Code:
Sub FilterDate()
    Dim Date1 As Date
    Dim lDate As Long, LastRow As Long


    Date1 = Date - 14


    lDate = DateSerial(Year(Date1), Month(Date1), Day(Date1))


    With Sheet1
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .AutoFilterMode = False
        With .Range("A6 :A" & LastRow)
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:=">=" & lDate, visibledropdown:=False
        End With
    End With


End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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