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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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