select multiple sheets to save as a pdf file in Excel VBA
Posted on 2014-10-02
I have the following code to save one sheet (the active one) as a pdf file. But I need convert it to save multiple sheets ( 2 out of 6 in this case) as one pdf file. I also know the sheet names I need to save. I need help to modify the following codes from save one sheet to save two sheets. Thanks in advance.
Public Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Dim User_Name As String
Dim FolderName As String
Set ws = ActiveSheet
'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
& "_" _
& Replace(ActiveWorkbook.FullName, ".xlsm", "_") _
& Format(Now(), "yyyymmdd\_hhmm") _
User_Name = Environ("username")
FolderName = "C:\Users\" & User_Name & "\Desktop\"
strFile = FolderName & strFile
myFile = Application.GetSaveAsFilename _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'Sheets("KE").Shapes("cmdSavetoPDF").Visible = False
If myFile <> "False" Then
' MsgBox "PDF file has been created."
' Sheets("KE").Shapes("cmdSavetoPDF").Visible = True
MsgBox "Could not create PDF file"