Anna Smith
Anna Smith

Reputation: 67

run-time error '1004': Autofilter method of range class failed

I have used a small code in vba which gives me this error::

Dim today As Date
Dim Mon As Integer

today = VBA.Date()
Mon = Month(today)

Sheets("Birthday List").Activate
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("A1:E1").AutoFilter , Field:=3, Criteria1:=Mon, Operator:=xlFilterValues
ActiveSheet.AutoFilterMode = True

Could you please help me how to correct it

regards Anna Smith

Upvotes: 0

Views: 20039

Answers (3)

ZawLin Htay
ZawLin Htay

Reputation: 1

Sub FilterWininwaFont()
        Dim ws As Worksheet
        Dim rng As Range
        Dim cell As Range
        Dim helperColumn As Range
        Dim LastRow As Long
        
        ' Change to your Sheet name
        Set ws = ThisWorkbook.Sheets("Sheet1")
        
        'Find the Last Row in column A (assuming column A has data)
        LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        
        'Set helper column next to your data (assuming column B is empty)
        Set helperColumn = ws.Range("B1:B" & LastRow)
        
        'Set header for helper column
        helperColumn.Cells(1, 1).Value = "Font Chectk"
        
        'Loop through each cell in the range and check font
        For Each cell In ws.Range("A2:A" & LastRow)
           If cell.Font.Name = "Win---inwa" Then
              cell.Offset(0, 1).Value = "Win---inwa"
           Else
              cell.Offset(0, 1).Value = ""
           End If
        Next cell
        
        ' Apply Filter
        ws.Range("A1:B" & LastRow).AutoFilter Field:=2, Criteria1:="Win---Inwa"
               
End Sub

Please reslove code Sir

Upvotes: 0

jackw454
jackw454

Reputation: 21

I'm not sure if this is already resolved (it's years old) but I had a similar error '1004': Autofilter method of range class failed today and it turned out that the data I was supposed to be filtering was in a table and that borked it. (The user who sends the report changed how they send it)

De-tabling the data (Convert Table to Range) resolved my issue. Maybe this will help someone else.

Upvotes: 2

Egan Wolf
Egan Wolf

Reputation: 3573

Problems in your code are:

  1. What @Cyril said in comments with update from @DavidZemens

"Autofilter , Field" where that ", " isn't needed and the criteria might need to be "=" & Mon

  1. You cannot set manually ActiveSheet.AutoFilterMode = True, but it's set automatically when calling ActiveSheet.Range("A1:E1").AutoFilter .... You can only set ActiveSheet.AutoFilterMode = False to turn the AutoFilter off.

Upvotes: 1

Related Questions