VBA code to export as pdf

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
888
Office Version
  1. 365
Platform
  1. Windows
I know I am writing this wrong (am getting an error) but am unsure how to modify it.
I am looking to export a sheet as pdf saving it as the filename specified in AB2 and directory specified in AB1.

Any help would be appreciated. Thank you!

VBA Code:
Dim Path As String
Dim filename As String
Path = Workbooks("Weekly Reprocesses WPG").Sheets("Weekly Reprocess").Range("AB1")
filename = Workbooks("Weekly Reprocesses WPG").Sheets("Weekly Reprocess").Range("AB2")
ActiveSheet.SaveAsFixedFormat Type:=xlTypePDF, filename:=Path & filename & ".pdf", Quality:=xlQualityStandard, _
  IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try ExportAsFixedFormat:

VBA Code:
Dim Path As String
Dim filename As String

Path = Sheets("Weekly Reprocess").Range("AB1")
filename = Sheets("Weekly Reprocess").Range("AB2")

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path & filename & ".pdf", Quality:=xlQualityStandard, _
  IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
 
Upvote 0
Solution
Hello Willow
I am literally on Day 1 of earning VBA and have tried your code.
Do you have any examples of what I should replace AB1 and AB2 with?
Thanks
Neil

AB1 is the Saved File Path, example: C:\Desktop\
AB2 is the File Name, example: My PDF

Hope this helps! :)
 
Upvote 0
mrshl9898: why would I be getting this error on the last line when I tried using this block of code?

1607446418980.png


I think it's a problem w/the path.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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