Hi all,
I am looking to create a macro that takes each sheet I have selected and turns it into a separate .pdf, and also names the .pdf.
For example, if there are five sheets in my workbook, but I only have three sheets selected, I would want those three sheets to be created into three separate .pdfs. The name of each .pdf would be located in one cell on each respective sheet.
Here is what I have so far. It currently works, but I can only do one sheet at a time. If I select multiple sheets, it only creates one .pdf and I would like each sheet to be its own .pdf. Any help would be appreciated!
I am looking to create a macro that takes each sheet I have selected and turns it into a separate .pdf, and also names the .pdf.
For example, if there are five sheets in my workbook, but I only have three sheets selected, I would want those three sheets to be created into three separate .pdfs. The name of each .pdf would be located in one cell on each respective sheet.
Here is what I have so far. It currently works, but I can only do one sheet at a time. If I select multiple sheets, it only creates one .pdf and I would like each sheet to be its own .pdf. Any help would be appreciated!
Code:
Sub Macro3()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim pdfName As String
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
pdfName = Range("O13").Text
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'create default name for savng file
strFile = pdfName & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub