Macro that creates separate .pdfs

ficktd

New Member
Joined
Apr 8, 2019
Messages
3
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!

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
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try;
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


    'get active workbook folder, if saved
    strPath = wbA.Path
    If strPath = "" Then
        strPath = Application.DefaultFilePath
    End If
    strPath = strPath & "\"


    For Each wsA In ActiveWindow.SelectedSheets


        pdfName = wsA.Range("O13").Text


        '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


    Next
exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub
 
Upvote 0
Thanks for the reply. This gets me closer to what I need, but each .pdf created contains the information for all tabs I have selected. I need each .pdf to contain only the information from its respective tab. Any ideas on how to update the macro to get each .pdf to contain only its respective information?
 
Upvote 0
It looks like the code makes two .pdfs if I have two sheets selected. When I look at the two .pdfs, they both contain two pages at at time. I am hoping to get it so each .pdf contains one page at a time.

I hope that makes sense.
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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