Macro to generate PDF files and save in folder

seekyourway

New Member
Joined
Nov 1, 2011
Messages
21
Hello,

I have a total of 20 tabs. Each tab name begins with 'Pb'. I want to generate 20 separate PDFs (only in printed area), one for each tab, and save 10 files in a folder location. (I have the printed area set up already in each tab)

I think I can borrow much of code below, but how do you add a loop, making it go through only tabs that begin with 'Pb'?

In the end, I want to save the 10 PDF files. If name is the same, overwrite. Otherwise, save as new.


Sub pdfsaver()

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
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet

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

strName = wsA.Range("A1").Value _
& " - " & wsA.Range("A2").Value _
& " - " & wsA.Range("A3").Value

'create default name for savng file
strFile = strName & ".pdf"
strPathFile = strPath & strFile

'export to PDF in current folder
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& strPathFile

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

Thank you
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hello,

Try this macro on a copy of your work. This should overwrite files without a prompt, so please test thoroughly on a copy of your work first. Be sure to change the path to wherever you'd like the PDFs to save.

VBA Code:
Sub Export_PDF()
Dim ws As Worksheet

Path = "C:\Users\Standard Tees\Documents\"

Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
    If Left(Trim(ws.Name), 2) = "Pb" Then
    ws.ExportAsFixedFormat xlTypePDF, _
    Filename:=Path & ws.Name & ".pdf"
    End If
Next ws
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Hello,

Try this macro on a copy of your work. This should overwrite files without a prompt, so please test thoroughly on a copy of your work first. Be sure to change the path to wherever you'd like the PDFs to save.

VBA Code:
Sub Export_PDF()
Dim ws As Worksheet

Path = "C:\Users\Standard Tees\Documents\"

Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
    If Left(Trim(ws.Name), 2) = "Pb" Then
    ws.ExportAsFixedFormat xlTypePDF, _
    Filename:=Path & ws.Name & ".pdf"
    End If
Next ws
Application.DisplayAlerts = True

End Sub

Thank you so much! It works
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,204
Members
453,022
Latest member
RobertV1609

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