How to convert SELECTED sheets only into separate PDF files

JuicyMusic

Board Regular
Joined
Jun 13, 2020
Messages
210
Office Version
  1. 365
Platform
  1. Windows
Gurus, I tried to figure out how to insert a line of coding at the DIM section and in the body of the code that will ONLY convert selected sheets into separate PDF files.
I have a code that runs perfectly....but it will generate a PDF for the sheets that I don't want to convert to PDF.

This code also places the PDF in whatever folder I am in.

Here is my code. I wish I could take the credit for it. I found it online, luckily. Thank you so so much. I can't wait to see how this is done.
VBA Code:
Sub ExportEachSheet2AsPDF()

Dim objWS As Worksheet
Dim strFileName As String
Dim strPath As String
Dim strCrntWS As String
 
strPath = ThisWorkbook.Path 'Assigned current file path
strCrntWS = ActiveSheet.Name 'Assigned current sheetname
 
Application.ScreenUpdating = False 'Disabled screen updating
 
For Each objWS In Worksheets
objWS.Select
 
strFileName = objWS.Name & ".pdf" 'Assigned worsksheet name as filename
 
'Following code will put '\' if it is not there
'-----------------------------------------------
If VBA.Right(strPath, 1) <> Application.PathSeparator Then
strPath = strPath & Application.PathSeparator
End If
 
'The following code will decide the export file type and then export it
'to the given path considering other required parameters.
'-----------------------------------------------------------------------
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=strPath & strFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False 'This will prevent to open the file after publishing
Next objWS
 
Worksheets(strCrntWS).Select 'After exporting all the worksheets, it will return to active sheet
Application.ScreenUpdating = True 'Enabled screen updating
 
MsgBox "All sheets have been exported as PDF.", vbInformation + vbOKOnly, "Exported as PDF"
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try replacing
VBA Code:
For Each objWS In Worksheets
with
VBA Code:
For Each objWS In ActiveWorkbook.Windows(1).SelectedSheets
 
Upvote 0
Solution
SOLVED. Thank you so much. I deactivated the original line of code with a " ' " and inserted your suggestion below it.
Just in case I need it for a different project or if someone would like to see this.

VBA Code:
Application.ScreenUpdating = False 'Disabled screen updating
   
'This section will create a PDF for all sheets. Selected or not
'For Each objWS In Worksheets
'objWS.Select

'This code will create a PDF for selected sheets only
For Each objWS In ActiveWorkbook.Windows(1).SelectedSheets
objWS.Select
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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