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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
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,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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