Save as PDF

elynoy

Board Regular
Joined
Oct 29, 2018
Messages
160
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
Hello. I have this code wich works fine for every sheet, I'd like to make it to save all sheets with one click only.

I need all the sheets in the workbook to be saved separately with the sheet name and the year given in a cell like the code shows. It works like a charm but I have to go on page by page and run the macro as it is.

Is it possible to make it loop the sheets and save them all with just one time click?

Code:
Here's the code I have:
Private Sub Save_As_PDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler


Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Worksheets("Alterações").Range("C17").Value _

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

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, "", "")
strName = Replace(strName, "", "")


'create default name for savng file
strFile = strName & "_" & 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 ( I tried to edit this part with another code but it just wont work or remoes the date option)
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub


Best regards,
eLy
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You need to loop through each worksheet. Change Set wsA = ActiveSheet to:
Code:
For Each wsA In wbA.Worksheets
and end the loop by adding
Code:
Next
above the exitHandler line.

Also, move the strPathFile and related lines and GetSaveAsFilename above the loop, otherwise it will prompt for the file for each sheet.

You could try adapting the code at https://www.mrexcel.com/forum/excel...ased-their-sheet-post5038663.html#post5038663, which saves each sheet as a separate PDF and names them as the sheet name plus some other cell data.
 
Last edited:
Upvote 0
Thank you for your help. I did what you said but If I change the code related to the second part of what you said so it wont prompt for each save, It wont work. Can you tell me how to achieve it?

eLy
 
Upvote 0
Code:
Option Explicit


Sub createPDFfiles()
    Dim ws As Worksheet
    Dim Fname As String
    For Each ws In ActiveWorkbook.Worksheets
        On Error Resume Next 'Continue if an error occurs


        ' Name PDF files based on the worksheet Index
        Fname = "Sheet" & ws.Index & " " & Sheets("Sheet1").Range("C17").Value
        
        ws.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=Fname, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False
    Next ws
End Sub
 
Upvote 0
your code works, but it gives me "sheet1", "sheet2".. I need the sheet name itself.

eLy
 
Upvote 0
I got it to work. This is how it is in case someone ever needs something similar.
Code:
Option Explicit


Sub createPDFfiles()
    Dim ws As Worksheet
    Dim Fname As String
    For Each ws In ActiveWorkbook.Worksheets
        On Error Resume Next 'Continue if an error occurs


        ' Name PDF files based on the worksheet Index
       Fname = ws.Name & "_" & Sheets("Alterações").Range("C17").Value
        
        ws.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=Fname, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False
    Next ws
End Sub

Thanks everyone.

Best regards,
eLy
 
Last edited:
Upvote 0
sorry, I just noticed that this saves the file in "My documents". I need it to save on the same folder as the workbook or at least let me choose like the first exemple I gave in the first post.

eLy
 
Upvote 0
.
Try this :

Code:
Option Explicit


Sub createPDFfiles()
    Dim ws As Worksheet
    Dim fname As String
    Dim myPath As String
    
    myPath = ThisWorkbook.Path
    Application.ScreenUpdating = False
    
    For Each ws In ActiveWorkbook.Worksheets
        
            On Error Resume Next 'Continue if an error occurs
    
            ' Name PDF files based on the worksheet Index
            fname = ws.Name & " " & Sheets("X").Range("C17").Value
    
            'PDF files are saved to same location as this workbook.
            '
            ws.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:=myPath & "\" & fname & ".pdf", _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False
        
    Next ws
    Application.ScreenUpdating = True
    
    MsgBox "Process completed. PDF files located in Workbook Folder. ", vbInformation, "Process Complete"
End Sub
 
Upvote 0
your code only returns the success message and wont do anything. but thanks for your herlp.

I ended up with this after i change the path. In case someone needs this:

Code:
Private Sub SaveAsPdfALL()    
    Dim ws As Worksheet
    Dim Fname As String
   
    For Each ws In ActiveWorkbook.Worksheets
        On Error Resume Next 'Continue if an error occurs


        ' Name PDF files based on the worksheet Index
       Fname = ThisWorkbook.Path & "\" & ws.Name & "_" & Sheets("SHEETXYZ").Range("C17").Value
        
        ws.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=Fname, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False
    Next ws
End Sub

Best regards,
eLy
 
Last edited:
Upvote 0
.
If the worksheets are blank, they won't be saved as a PDF. There must be some kind of data on each sheet in order for it to be saved.

The macro I posted works here - when there is data on each sheet. There is no need to save an empty sheet.
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,981
Members
449,058
Latest member
oculus

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