VBA - Save Worksheets as Individual PDFs

Small Paul

Board Regular
Joined
Jun 28, 2018
Messages
118
Hi

I have searched various websites/forums and tried the solutions but without success. I am hoping somebody here can help on this issue.

The code below basically has 3 stages:
1) Create a new folder and save the master file (this works properly)
2) Save the active sheet as a pdf (this does not work AT ALL)
3) Select next worksheet (this works properly)


Code:
Sub Comms_Statements_Save_As_PDFs()'
' Save_As_PDFs Macro


    Dim Path As String
    Dim d As String
    Dim sh As Worksheet


    Dim WS_Count As Integer
    Dim strFolder As String
    Dim strFile As String
    Dim I As Integer
    d = Format(Date, "yyyy-mm-dd")
    
'create folder
    strFolder = "Z:\Paul\Commission Paid\"
    strFile = activesheet.range("B4").value
    strFolder2 = "Z:\Paul\Commission Paid\" & d
    
    Path = "Z:\Paul\Commission Paid\"
    If Len(Dir(Path, vbDirectory)) = 0 Then
    MsgBox "Path does not exist.", vbCritical
    Exit Sub
    End If
    If Len(Dir(Path & d, vbDirectory)) = 0 Then MkDir (Path & d)
    ActiveWorkbook.SaveAs Filename:=strFolder & d & "\" & " " & "Commission Statements" & ".xlsm"
    Sheets("1").Activate


'Convert Each Sheet to PDF and Save
    WS_Count = ActiveWorkbook.Worksheets.Count
    For I = 3 To Sheets.Count
    range("B4").Select
    activesheet.ExportAsFixedFormat Type:=xlTypePDF
    Filename = strFolder2 & "\" & strFile & ".pdf"
    quality = xlQualityStandard
    includedocproperties = True
    ignoreprintareas = False
    openafterpublish = False
    I = I + 1
    activesheet.Next.Select
    Next
    End Sub

I need:
- each worksheet (from 3 to 'variable') to be saved as an individual PDF
- in the newly created folder - Z:\Paul\Commission Paid\[today's date]
- with a file name using the contents of cell B4 on each worksheet

Any and all assistance appreciated.

Cheers
Small Paul.
 
You're missing the . from in front of the range("I"...
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
OFFS!
I think I have mentioned to you before that I am useless and I guess this proves it.

Thank you for ending 4 hours of head scratching :cool:
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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