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

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.

Forum statistics

Threads
1,212,938
Messages
6,110,771
Members
448,297
Latest member
cocolasticot50

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