VBA error: 1004 when autofiltering

Tanyaann1995

Board Regular
Joined
Mar 24, 2021
Messages
62
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I'm using the below code to use filter on a bunch of data in a worksheet. However, the error 1004 (Application defined or object defined error) keeps popping up after the Autofilter statement (highlighted below) when I execute this code. I can't figure out where I'm making the error here. Pls help.

Sub historical()

Dim i As Integer
Dim lRow As Long
Dim pno As String
Dim name As String
Dim Max_date As Date
Dim pfind As Range
Dim m As Long
Dim n As String

lRow = ThisWorkbook.Worksheets(2).Range("E:E").Find(what:="*", _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row

name = InputBox("Input customer name to search against")

For i = 24 To lRow
If IsEmpty(Cells(i, 4).Value) = False Then
pno = Cells(i, 4).Value
With Workbooks("Latest hist prices 3.xlsx").Worksheets(1)
.Range("A2:DV25290").AutoFilter Field:=67, Criteria1:=pno
.Range("A2:DV25290").AutoFilter Field:=15, Criteria1:=name
Max_date = Application.WorksheetFunction.Max(.Columns("CA"))
Set pfind = .Range("CA:CA").Find(what:=Max_date, LookIn:=xlValues, LookAt:=xlWhole)
pfind.Offset(0, -27).Value = m
pfind.Offset(0, -28).Value = n
ThisWorkbook.Worksheets(2).Cells(i, 21).Value = m & n & "-" & Max_date
End With
End If
Next i


End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Replacing the 2 filter lines with
VBA Code:
    With .Range("A2:DV25290")
        .Autofilter
        .AutoFilter Field:=67, Criteria1:=pno
        .AutoFilter Field:=15, Criteria1:=name
    End With
Without seeing the sheet, best guess would be that there is an existing filter of less than 67 columns applied to part of the same range. If that is the case then clearing it first should resolve the issue.
 
Upvote 0
Solution
Replacing the 2 filter lines with
VBA Code:
    With .Range("A2:DV25290")
        .Autofilter
        .AutoFilter Field:=67, Criteria1:=pno
        .AutoFilter Field:=15, Criteria1:=name
    End With
Without seeing the sheet, best guess would be that there is an existing filter of less than 67 columns applied to part of the same range. If that is the case then clearing it first should resolve the issue.
Hi,
Thanks, this code worked :)
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

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