Printout only visbile sheet in Array

jamesmcintyre

New Member
Joined
Nov 16, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello, i am trying to run a macro that print an array of sheets in the description. However i have a macro that hides certain sheets pending other options and when i do the printout option it comes up with an error if one of the ****s has been hidden. The sheets need to remain hidden depending on the options.

The code i currently have is below.

Sub PrintSpecificSheets()
ThisWorkbook.Worksheets(Array("D 30%", "EW 30%", "RW 30%", "S 30%", "W 30%")).PrintOut
End Sub

Is there an option i can include that only prints the visible sheets out of whats included in the above array?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi there...

Try the code below...

VBA Code:
Sub PrintSpecificSheets()
    Dim SH As Worksheet
    For Each SH In Worksheets(Array("D 30%", "EW 30%", "RW 30%", "S 30%", "W 30%"))
        If SH.Visible = xlSheetVisible Then
            SH.PrintOut Copies:=1, Collate:=True
            Exit For
        End If
    Next SH
End Sub
 
Upvote 0
Thank you, it has partly worked. Its not throwing up the error if 1 is hidden but its not printing all the visible ones still.
 
Upvote 0
Thanks for the feedback... Not at PC at the moment, maybe someone else can also jump in and see where it went wrong?
 
Upvote 0
@jamesmcintyre

Try updated...

VBA Code:
Sub PrintSpecificSheets()
    Dim SH As Worksheet
    For Each SH In Worksheets(Array("D 30%", "EW 30%", "RW 30%", "S 30%", "W 30%"))
        If SH.Visible = xlSheetVisible Then
            SH.Select
            SH.PrintOut
        End If
    Next SH
End Sub
 
Upvote 0
Almost there, i just need to figure out how to get it to do as a combined pdf rather then all individually. I really appreciate your help.
 
Upvote 0
What do you mean by combined pdf? Do you want to export the selected sheets to a pdf file?
 
Upvote 0
Yes please. The idea is that only the visible sheets in the array are exported as pdf and all sheets are combined the export if possible.
 
Upvote 0
Try:

VBA Code:
Sub PrintSpecificSheets()
On Error GoTo errorhandler
    Dim SH As Worksheet
    For Each SH In Worksheets(Array("D 30%", "EW 30%", "RW 30%", "S 30%", "W 30%"))
        If SH.Visible = xlSheetVisible Then
            SH.Select
ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "exported file.pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
        End If
    Next SH
errorhandler:
    Exit Sub
End Sub
 
Upvote 0
Combined perfectly although when one of the sheets in the array isnt visible the print applies to all sheets in the workbook and not the array.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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