[VBA] Print to PDF

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
98
Office Version
  1. 365
Platform
  1. Windows
Good day,

I currently have the below VBA code, which runs exactly as I need it to. It goes through a dropdown list and physically prints off the each result.
VBA Code:
Sub Macro1()
    Dim r As Range
    For Each r In Range(Range("J2").Validation.Formula1)
        Range("J2").Value = r.Value
        Sheets("Sales-Referrals").PrintOut
    Next
End Sub

What I want to do is update it to instead of printing to a printer, I want it to print each result to a PDF file, save it to the Desktop and name each individual file based off of cell B2.

Any assistance would be greatly appreciated!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This macro should do it:
VBA Code:
Public Sub Create_PDFs()

    Dim DesktopFolder As String
    Dim r As Range
    
    DesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"
    
    For Each r In Range(Range("J2").Validation.Formula1)
        Range("J2").Value = r.Value
        Sheets("Sales-Referrals").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=DesktopFolder & Sheets("Sales-Referrals").Range("B2").Value & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,183
Messages
6,123,529
Members
449,105
Latest member
syed902

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