VBA Error "Run-Time error'1004': PrintOut Method of Worksheet class failed

ranie

New Member
Joined
Apr 1, 2017
Messages
11
Hello all,
This is the VBA code that I have to loop the dropdown list and print. I am getting an error message when I click cancel during the loop. How do I disable or not show the error message?
VBA Error "Run-Time error'1004': PrintOut Method of Worksheet class failed
It asks me to debug ActiveSheet.Printout line


Sub Test()

Dim R As Range
For Each R In Sheets("PNL").Range("s9:s32")
If Not IsEmpty(R) Then
Range("B1:C1") = R
ActiveSheet.PrintOut

End If
Next
End Sub

Thank you very much
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Sub Test()

Dim R As Range
For Each R In Sheets("PNL").Range("s9:s32")
If Not IsEmpty(R) Then
Range("B1:C1") = R
ActiveSheet.PrintOut

End If
Next
End Sub
You have a few options. You can add error handling code to handle that kind of error (do a Google Search on "Excel VBA error handling" or you can simply ignore it, like this:
VBA Code:
Dim R As Range
For Each R In Sheets("PNL").Range("s9:s32")
    If Not IsEmpty(R) Then
        Range("B1:C1") = R
        On Error Resume Next
        ActiveSheet.PrintOut
        On Error GoTo 0
    End If
Next

End Sub
 
Upvote 0
Dim R As Range For Each R In Sheets("PNL").Range("s9:s32") If Not IsEmpty(R) Then Range("B1:C1") = R On Error Resume Next ActiveSheet.PrintOut On Error GoTo 0 End If Next End Sub
Thanks, but I will continue the loop. is there a way to stop the loop in the middle on cancel?
 
Upvote 0
OK, then we should probably use error handler there, i.e.
VBA Code:
Dim R As Range

On Error GoTo err_chk
For Each R In Sheets("PNL").Range("s9:s32")
    If Not IsEmpty(R) Then
        Range("B1:C1") = R
        ActiveSheet.PrintOut
    End If
Next
On Error GoTo 0

Exit Sub

err_chk:
    If Err.Number = 1004 Then
        MsgBox "Printing has been cancelled"
        Exit Sub
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If
    
End Sub
 
Upvote 0
Solution
Dim R As Range On Error GoTo err_chk For Each R In Sheets("PNL").Range("s9:s32") If Not IsEmpty(R) Then Range("B1:C1") = R ActiveSheet.PrintOut End If Next On Error GoTo 0 Exit Sub err_chk: If Err.Number = 1004 Then MsgBox "Printing has been cancelled" Exit Sub Else MsgBox Err.Number & ":" & Err.Description End If End Sub
This is perfect!!!!!! thanks!
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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