VBA to print multiple sheets to PDF, but some based on cell value!

andyc6610

New Member
Joined
Mar 3, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to write a VBA code that allows me to print multiple sheets to a PDF - requirements are as follows:
  • Sheet1 is an index.
    • Column B contains a list of all other Sheet names in column 2 as hyperlinks (about 120 other sheets)
    • Column C contains numerical values which relate to each of these sheets
  • I want to be able to print one PDF with the following
    • Index at front every time (Sheet1)
    • Print all other sheets if Column C in Sheet1 contains a value >0

This means that for Sheet2 to Sheet1**, they will only form part of the PDF if the cell value in the index is >0. The same value appears in every tab as cell C2, so could use that as a reference.

For example:
1677856110199.png


In this case I would want to print Sheet1 as index, but also Sheet2 and Sheet4 based on the cell values of the index.

I tried using
VBA Code:
For Each ws In Worksheets

But that considers all sheets and Sheet1 should be excluded as always printed

I also have following code which is probably still useful:
Code:
    'Grouping selected worksheets
    Worksheets(Split(Left(strWS, Len(strWS) - 1), cstrDel)).Select
    'Printing to PDF
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=ActiveWorkbook.Path & "\" & "TestPrintAllActiveToPDF" & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True


Any advice would be appreciated!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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