Error Handling Help

Joined
Mar 22, 2007
Messages
4
Reaction score
0
Greetings,

I have a workbook with multiple sheets. This is the code I'm using to hide unused rows, where AutoFilter looks down a column and if it's blank, hides the corresponding row.

This code is for 4 of the sheets. But if you select another worksheet, I'd like it to go to the error handler? I had to unprotect, then re-protect the sheet to use the AutoFilter of course

Any ideas or possible code scrubbing, would be greatly appreciated.

Thanks,
SK

Private Sub Hide_PCN_Rows()
'
'
'
If ActiveWorkbook.Name = ThisWorkbook.Name Then
Application.ScreenUpdating = False

On Error GoTo Handler

If ThisWorkbook.Worksheets("PCN4").Visible = True Then
ActiveSheet.Select
ActiveSheet.Unprotect Password:="secret"
Selection.AutoFilter Field:=1, Criteria1:="<>"
Range("C18").Select
ActiveWindow.LargeScroll Up:=10
ActiveWindow.LargeScroll ToLeft:=3
ActiveSheet.Protect Password:="secret"

ElseIf ThisWorkbook.Worksheets("PCN3").Visible = True Then
ActiveSheet.Select
ActiveSheet.Unprotect Password:="secret
Selection.AutoFilter Field:=1, Criteria1:="<>"
Range("C18").Select
ActiveWindow.LargeScroll Up:=10
ActiveWindow.LargeScroll ToLeft:=3
ActiveSheet.Protect Password:="secret"

ElseIf ThisWorkbook.Worksheets("PCN2").Visible = True Then
ActiveSheet.Select
ActiveSheet.Unprotect Password:="secret"
Selection.AutoFilter Field:=1, Criteria1:="<>"
Range("C18").Select
ActiveWindow.LargeScroll Up:=10
ActiveWindow.LargeScroll ToLeft:=3
ActiveSheet.Protect Password:="secret"

ElseIf ThisWorkbook.Worksheets("PCN1").Visible = True Then
Sheets("PCN1").Select
Sheets("PCN1").Unprotect Password:="secret"
Selection.AutoFilter Field:=1, Criteria1:="<>"
Range("C18").Select
ActiveWindow.LargeScroll Up:=10
ActiveWindow.LargeScroll ToLeft:=3
Sheets("PCN1").Protect Password:="secret"

End If

Application.ScreenUpdating = True

End If

Exit Sub
Handler:
MsgBox "There are no PCN rows on this sheet." & vbCrLf _
& "Please select a PCN sheet and try again."


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top