Print Specific Sheets, if sheets does not exist, skip

vbanoob1234

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

I am having difficulties. I have a workbook with several worksheets, Sheet 1, Sheet 2, Sheet 3.
But some workbooks have Sheet 3 missing, so therefore I want my macro to skip that worksheet and continue.
Here is what I got so far.

Sub SpecficSheets()


Dim wb As Workbook, ws As Worksheet
Dim sFil As String, sPath As String
Set wb = ActiveWorkbook
Set ws = ActiveSheet






sPath = "C:Desktop\Print All" '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 to set recon page to one
With Worksheets("T3 Class").PageSetup
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With


'ws.PrintOut
wb.Sheets(Array("Sheet 1", "Sheet 2", "Sheet 3")).Select
wb.Worksheets(Array("Sheet 1", "Sheet 2", "Sheet 3")).PrintOut
wb.Close


sFil = Dir()
Loop
Application.DisplayAlerts = True


End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Or use this method
Replace this
Code:
'ws.PrintOut
 wb.Sheets(Array("Sheet 1", "Sheet 2", "Sheet 3")).Select
 wb.Worksheets(Array("Sheet 1", "Sheet 2", "Sheet 3")).PrintOut
With this
Code:
'ws.PrintOut
  For Each ws In wb.Sheets    
   ws.PrintOut
 Next
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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