OnError Resume Next after autofilter

Eclektics

New Member
Joined
Jun 9, 2015
Messages
24
Hi all,

I'm trying to handle an event with an on error line but it seems to be skipping it. I've checked my options and it's still set to only break on unhandled errors so I'm not sure what's causing it. My main aim is to filter a bunch of data and delete the visible rows, on occasion the value I'm filtering for won't exist in the data set so I want a procedure to handle this and move on to the rest of the process.

Here's my code, I've tried different options from resume next to GoTo and still no joy, the first part relating to CCN works but then breaks at the second section with a runtime 1004 error 'no cells visible'and highlights line '
.Range("$A$2:$AW$" & LRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete' when debugging.
Any help anyone can offer would be greatly appreciated. :)

Code:
Sub Test()
Dim ws As Worksheet
Set ws = Sheet2
Dim LRow As Long
LRow = ws.Cells(ws.Rows.Count, "A:A").End(xlUp).Row
With ws
    On Error GoTo 66
    .Range("$A:$AW").AutoFilter field:=14, Criteria1:="CCN*"
    .Range("$A$1:$AW$" & LRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws.Range("A1")
    .Range("$A$2:$AW$" & LRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
66
    .ShowAllData
End With
With ws
    .Range("$A:$AW").AutoFilter field:=27, Criteria1:="*PECB*"
On Error GoTo nextline:
    .Range("$A$2:$AW$" & LRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
nextline:
    .ShowAllData
End With
End Sub
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
you turned 'On Error GoTo nextline' into a label with the colon.
remove it.
On Error GoTo nextline

Nextline IS a label.
And you need to end the code if you DONT want nextline to run. As is, it runs it anyway.

Code:
On Error GoTo nextline
With ws
    .Range("$A:$AW").AutoFilter field:=27, Criteria1:="*PECB*"
    .Range("$A$2:$AW$" & LRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
exit sub

nextline:
    ws.ShowAllData
End Sub



<strike></strike>
 
Last edited:
Upvote 0
Thanks for your quick response :) I've amended the code as you've advised but it still errors on the same line and doesn't skip?
 
Upvote 0
VBA can only handle one error at a time, so if you had an error here
Code:
    On Error GoTo 66
    .Range("$A:$AW").AutoFilter field:=14, Criteria1:="CCN*"
    .Range("$A$1:$AW$" & LRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws.Range("A1")
    .Range("$A$2:$AW$" & LRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
66
as you haven't cleared the error, the next error handler won't work.
Have a look here for info on errors & error handling http://excelmatters.com/2015/03/17/on-error-wtf/

Try
Code:
Sub Test()
Dim ws As Worksheet
Set ws = Sheet2
Dim LRow As Long
LRow = ws.Cells(ws.Rows.Count, "A:A").End(xlUp).Row
With ws
    .Range("$A:$AW").AutoFilter field:=14, Criteria1:="CCN*"
    On Error Resume Next
    .Range("$A$1:$AW$" & LRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws.Range("A1")
    .Range("$A$2:$AW$" & LRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    On Error GoTo 0
    .ShowAllData
    
    .Range("$A:$AW").AutoFilter field:=27, Criteria1:="*PECB*"
    On Error Resume Next
    .Range("$A$2:$AW$" & LRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    On Error GoTo 0
    .ShowAllData
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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