VBA - Debugging Print Error b.c of Hidden/Very Hidden Sheets

vbanoob1234

New Member
Joined
Aug 8, 2016
Messages
26
Hello everyone,

This is my code. This code will grab all the files from a source folder, open each workbook, print all worksheets, and then close it. repeat.

However, I am getting an error saying "Run Time Error 1004. Method "PrintOut" of object_Worksheet. failed.

I think it is because there are very hidden and hidden sheets on the workbook.
Any help is appreciated. Thanks
Rich (BB code):
Dim sFil As String, sPath As String


Set wb = ActiveWorkbook
Set ws = ActiveSheet


'change path to match yours


sPath = "C:\Desktop" 'location of files
sFil = Dir(sPath & "*.xlsx") 'change or add formats


Application.DisplayAlerts = False
Do Until sFil = ""
Workbooks.Open sPath & sFil

Set wb = ActiveWorkbook

'ws.PrintOut

For Each ws In wb.Worksheets
If Worksheet.Visible Then ws.PrintOut
Next
wb.Close


sFil = Dir()
Loop
Application.DisplayAlerts = True


End Sub

Thank you
 
Last edited by a moderator:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Presumably you've defined Worksheet, if you're not getting an error with Worksheet.Visible?

But it looks like you want:

Code:
For Each ws In wb.Worksheets
    If [COLOR=#ff0000][B]ws[/B][/COLOR].Visible Then ws.PrintOut
Next
 
Upvote 0
The .Visible property of a worksheet is NOT a boolean value.
Change the code to
Code:
For Each ws In wb.Worksheets
    If ws.Visible = xlSheetVisible Then ws.PrintOut
Next


If you run this test code on a worksheet with two worksheets, I think you might be surprised by the third message box

Code:
Sub test()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    ws.Visible = xlSheetVisible
    If ws.Visible Then
        MsgBox "xlSheetVisible, .Visible=True"
    Else
        MsgBox "xlSheetVisible, .Visible=False"
    End If
    
    ws.Visible = xlSheetHidden
    If ws.Visible Then
        MsgBox "xlSheetHidden, .Visible=True"
    Else
        MsgBox "xlSheetHidden, .Visible=False"
    End If
    
    ws.Visible = xlSheetVeryHidden
    If ws.Visible Then
        MsgBox "xlSheetVeryHidden, .Visible=True"
    Else
        MsgBox "xlSheetVeryHidden, .Visible=False"
    End If
End Sub
 
Upvote 0
Hi mikerickson

Thank you for your code. It worked perfectly. Much appreciated.

However, I am not understanding your sub test() code,
Could you elaborate? It just deletes one of my sheet when I run it.

Thanks
 
Upvote 0
If you look at the third IF, you'll see that the

If ws.Visible Then goes to the True branch when ws is VeryHidden. Its a demonstration of the inaccuracy of testing ws.Visible as a Boolean and the need to test ws.Visible against xlSheetVisiblibility constants.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,292
Members
449,218
Latest member
Excel Master

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