VBA to print all selected sheets

ExcelAtEverything

Active Member
Joined
Jan 30, 2021
Messages
351
Office Version
  1. 2019
Platform
  1. Windows
I have code already that selects 10 of the 20 or so worksheets in my workbook. The next part of the code (shown below) is what I need help with.

As it stands, this code below will print every worksheet in my workbook as a PDF, and name each PDF as the same name as the sheet it was printed from (the name of the tab/worksheet).
But I need to change it so that it only prints a PDF copy of each sheet that's currently selected, and names each PDF as (Worksheet name) & the date. The date needs to be the date that exists already in the workbook in cell $G$3 of "Sheet1", if that's possible.

Dim ws As Worksheet
Dim folderPath As String

folderPath = "C:\Users\myname\Downloads\"

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

For Each ws In ThisWorkbook.Worksheets
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=folderPath & ws.Name & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Next
'
End Sub


Thanks!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi ExcelAtEverything,

maybe try and wrap the code in Code-Tags instead of formatting it as bold. ;)

Please try
Code:
Dim objSheet As Worksheet
Dim folderPath As String

folderPath = "C:\Users\myname\Downloads\"

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

For Each objSheet In ActiveWindow.SelectedSheets
  objSheet.ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=folderPath & objSheet.Name & Worksheets("Sheet1").Range("G3").Text & ".pdf", _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False
Next objSheet
As the name of the worksheet and the date will be printed without gap you may want to add a blank between these two (or revert them).

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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