Exporting selected sheets to pdf

StianU

New Member
Joined
Jan 25, 2019
Messages
4
Hi,

I have a code which i suppose to print the sheets I have selected to pdf (the list of the sheetnames are in cell A1). But my code only prints blank pages (or the singled marked cells on each sheet). How do I solve this?
Here's the code:

Sub PrintSelectedSheets()

Dim wrLocal As Workbook
Dim shInput As Worksheet
Dim Sheetnames As Variant
Dim strFilename As String
Dim strFiledir As String
Dim Ans As Integer

Set wrLocal = ThisWorkbook
Set shInput = wrLocal.Worksheets("INPUT")

Sheetnames = Split(shInput.Range("A1"), ",")
Worksheets(Sheetnames).Select

strFiledir = ActiveWorkbook.Path & "\PDF"

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

strFilename = Range("filename").Value & ".pdf"

If Dir(strFiledir & strFilename) <> "" Then
Ans = MsgBox("Filename exists. Overwrite?", vbQuestion + vbYesNo, "Overwrite?")
If Ans = vbNo Then Exit Sub
End If

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFiledir & strFilename _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True

Sheets("INPUT").Select

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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