Error Handling - Advanced Filter

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Looking for assistance in error handling when an advanced filter turns up negative results for deletion.

Here is a snippit of code ...
Rich (BB code):
With Worksheets("Data")
        If .FilterMode Then .ShowAllData
        llastrow = Range("a65536").End(xlUp).Row
        With .Range("A1:M" & llastrow)
            .AdvancedFilter _
                    Action:=xlFilterInPlace, _
                    CriteriaRange:=Worksheets("Criteria").Range("A1:D2"), _
                    Unique:=False
            Set rngToDelete = .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
            lredrow = rngToDelete.Count
            ' MsgBox ("Redundant Rows Found: ") & lredrow
            Sheets("Main").Activate
            Range("D7").Value = lredrow
                 
        End With

"No cells were found." Error at red highlighted line.

Jenn
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You can do something like:

Rich (BB code):
With Worksheets("Data")
        If .FilterMode Then .ShowAllData
        llastrow = Range("a65536").End(xlUp).Row
        With .Range("A1:M" & llastrow)
            .AdvancedFilter _
                    Action:=xlFilterInPlace, _
                    CriteriaRange:=Worksheets("Criteria").Range("A1:D2"), _
                    Unique:=False
            On Error GoTo Handler
            Set rngToDelete = .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
            lredrow = rngToDelete.Count
            ' MsgBox ("Redundant Rows Found: ") & lredrow
            Sheets("Main").Activate
            Range("D7").Value = lredrow
                 
        End With
Exit sub
Handler:
msgbox "No cells were found!"
End Sub

Hope that helps.
 
Upvote 0
Thank you schielrn, this is doing the trick, however, I have two considerations that need to be dealt with.

1) When it reaches the error, it's great to advise the user with the MsgBox, but how do I resume the code after the error? There is more to the sub after the filter application.
2) I have several advanced filters applied at different points in this sub. Does this improvement handle similar (no cells found) errors in the additional filters? If so, how would I go about differentiating the MsgBox to indicate the specific filter results (ie. no x rows found; no y rows found etc ... where x and y etc. are different filters)?

Jenn
 
Upvote 0
You could possibly put in an On Error Resume Next statement, but that will skip over all errors and may do some things you do not want it to do?

Rich (BB code):
With Worksheets("Data")
        If .FilterMode Then .ShowAllData
        llastrow = Range("a65536").End(xlUp).Row
        With .Range("A1:M" & llastrow)
            .AdvancedFilter _
                    Action:=xlFilterInPlace, _
                    CriteriaRange:=Worksheets("Criteria").Range("A1:D2"), _
                    Unique:=False
            On Error Resume Next
            Set rngToDelete = .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
            lredrow = rngToDelete.Count
            ' MsgBox ("Redundant Rows Found: ") & lredrow
            Sheets("Main").Activate
            Range("D7").Value = lredrow
                 
        End With
Try that on a copy and see if it is doing what you want, otherwise we can maybe explore other options.
 
Upvote 0
Hi schielrn,

I liked the Handler: method as it allowed me to provide some user notification as well as some other code (which is why it's important to know whether this same code will handle all the advanced filter errors in the same way, which will not provide the results I need).

I have added Resume Next as the last line of my Handler: code, but what is happening when I run the code now, and it finds an error, it executes the Handler: code twice. It goes through it once, returns to the next line, and does it again, and then resumes the rest of the code as it should. No need for the second error handling.

Jenn
 
Upvote 0

Forum statistics

Threads
1,222,141
Messages
6,164,165
Members
451,880
Latest member
2da

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