VBA Autofilter when result is nothing ERROR in the code

reddsable

New Member
Joined
Jun 3, 2019
Messages
12
I have the below code and I get an error when trying to run it I get the error

Run-time error '1004':Autofilter method of Range class failed

This is because after filtering sometimes I have no results.
I would like to have a code that tells to ignore the code if the Autofilter returns no result. I have tryied also including the code
On Error Resume Next
On Error GoTo 0
Any idea how to do that? Thanks!
<strike>
</strike>
<strike></strike>​[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Worksheets("DATA").AutoFilterMode = False
If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A1").AutoFilter
End If

Selection.AutoFilter Field:=95, Criteria1:=Array("ABC", "DEF", "GHI", "JKL"), Operator:=xlFilterValues

ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count

If ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then

ActiveSheet.Range("2:" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter

End If
<strike></strike>
[/FONT]
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Welcome to the Board!

Put the "On Error Resume Next" line before the filter code, and the "On Error GoTo 0" line after the filter code.
 
Upvote 0
If I do that, it's deleting my data that shouldn't be deleted, because the filter did not work, considering there were no values "ABC", "DEF" etc so the filter did not filter any data, but all my data was deleted
 
Upvote 0
Maybe something like
Code:
   With ActiveSheet
      .Range("A1").AutoFilter 95, Array("ABC", "DEF"), xlFilterValues
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
 
Upvote 0
So, is it that you only want to delete the rows containing "ABC", "DEF", "GHI", or "JKL"?
 
Upvote 0
So, is it that you only want to delete the rows containing "ABC", "DEF", "GHI", or "JKL"?

Yes I want to delete only the rows containing
"
ABC", "DEF", "GHI", or "JKL", but sometimes it happens in my reports that I do not have these values. So when I don't have the values the code is deleting all my existing data
 
Upvote 0
Take a look at the code that Fluff posted and see if that does it for you (though I think you will need to add the other values in your array to his code, I am guessing he just did a simple test on the two).
 
Upvote 0
Finally I added my other values and it seems to be working. Thanks everybody!

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Worksheets("DATA").AutoFilterMode = False
If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("A1").AutoFilter
End If[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Selection.AutoFilter Field:=14, Criteria1:=Array("ABC", "DEF", "GHI", "JKL"), Operator:=xlFilterValues[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]If ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]ActiveSheet.Range("2:" & ActiveSheet.UsedRange.Rows.Count).SpecialCells(xlCellTypeVisible).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Else: Selection.AutoFilter Field:=14, Criteria1:=Array _
("MNO", "PQR", "STU"), Operator:=xlFilterValues[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
End If[/FONT]
 
Upvote 0
A word of warning, that will only work correctly if if row 2 contains one of the filtered values.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

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