PDA

View Full Version : VBA Code for PDF of Vibile Sheets in array



Jimmcintyre
11-20-2022, 04:12 PM
Hello, i am trying to run a macro that PDFs 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 sheets 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 PDFs the visible sheets out of whats included in the above array?

It would be perfect if the PDF of the visible sheets is combined of all sheets in the array that are visible and visible only.

June7
11-20-2022, 04:51 PM
I expect you need to dynamically build the array with only visible sheets.

Jimmcintyre
11-20-2022, 05:19 PM
Is there any code that can search the array and pick out the visible sheets in the array to then only print them combiend.

The array is large and cant be updated each time to print.

June7
11-20-2022, 07:52 PM
You show only 5 elements in array. That is not large. How many worksheets are there?

Would have to test if each sheet is visible and conditionally build array string.

Jimmcintyre
11-20-2022, 09:12 PM
This code is to be applied to multiple worksheets with different arrays.

June7
11-21-2022, 01:02 AM
Doesn't alter my comments.

georgiboy
11-21-2022, 01:29 AM
Is there any code that can search the array and pick out the visible sheets in the array to then only print them combiend.

The array is large and cant be updated each time to print.



Sub PrintSpecificSheets()
Dim wsArr As Variant, wsC As Integer, z As Integer, oVar() As Variant

wsArr = Array("D 30%", "EW 30%", "RW 30%", "S 30%", "W 30%")

For wsC = 0 To UBound(wsArr)
If Sheets(wsArr(wsC)).Visible Then
ReDim Preserve oVar(z): oVar(z) = wsArr(wsC): z = z + 1
End If
Next wsC

Worksheets(oVar).PrintOut
End Sub