On error resume next not working

C

cluckers

the following code is supposed to print sheet2 if there is a sheet two, then
delete sheet2. So the first part checks if there is content on sheet2("A1")
and prints it. If ther is no content on sheet2 the error handler skips this
step. Then it is supposed to delete sheet2. If there is no sheet2 the error
handler resume next is supposed to skip this part but it is not. If there is
no sheet2 the line-Worksheets("Sheet2").Delete, returns a runtime error 9.

Why does the on error resume next not working? Thanks

Sub print()
On Error GoTo skipprint

If Not Worksheets("Sheet2").Range("A1").Value = "" Then
If MsgBox("You have trades that need to be placed manually. Do you want to
print them?", vbYesNo) = vbYes Then
Worksheets("Sheet2").Rows.AutoFit
Worksheets("Sheet2").Columns.AutoFit
Worksheets("Sheet2").PrintOut

Else
skipprint:
End If
End If

MsgBox ("If prompted select 'Delete' or this will not work.")
On Error Resume Next
Worksheets("Sheet2").Delete

End Sub
 
J

Jacob Skaria

Try the below...Please note that there is a separate function to check
whether the sheet exists or not...

Sub MacroPrint()
If SheetExist("Sheet2") Then
If Worksheets("Sheet2").Range("A1") = "" Then
If MsgBox("You have trades that need to be placed manually." & _
"Do you want to print them?", vbYesNo) = vbYes Then
Worksheets("Sheet2").Rows.AutoFit
Worksheets("Sheet2").Columns.AutoFit
Worksheets("Sheet2").PrintOut Copies:=1, Collate:=True
End If
End If
Application.DisplayAlerts = False
Sheets("Sheet2").Delete
Application.DisplayAlerts = True
End If
End Sub

Function SheetExist(strSheet As String) As Boolean
On Error Resume Next
SheetExist = Not (Sheets(strSheet) Is Nothing)
End Function

If this post helps click Yes
 
C

cluckers

This works great. Not exactly sure how the function works though. Currious
as to why my on error resume next was not working. Do you know?
 

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

Top