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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,491
Messages
6,125,110
Members
449,205
Latest member
ralemanygarcia

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