Exporting to PDF Macro - Naming Query

dawhite

New Member
Joined
Jul 8, 2022
Messages
3
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Hi All,

I'm attempting to download all excel worksheets as pdf files using a macro (as per below), however the file names of the exported files are based off the worksheet names and I need them to be based off the contents of specific cells instead.

Can anybody help me to name the files according to specific cells rather than the worksheet names please?


Sub Export_All_Invoices()

Dim Folder_Path As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the Folder Path"

If .Show = -1 Then Folder_Path = .SelectedItems(1)


End With

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets

sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & sh.Name & ".pdf"

Next

MsgBox "Done"


End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
replace with this
sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & sh.range("<your cell ref here.").value & ".pdf"
 
Upvote 0
replace with this
sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & sh.range("<your cell ref here.").value & ".pdf"
Thanks so much for your response. Unfortunately, this now comes up with an error as per the attached image.
 

Attachments

  • Macro.PNG
    Macro.PNG
    49.5 KB · Views: 22
Upvote 0
Hmm strange worked perfectly on my side isthere definitely a value within the range on all sheets of your workbook?


Sub Export_All_Invoices()

Dim Folder_Path As String

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the Folder Path"

If .Show = -1 Then Folder_Path = .SelectedItems(1)


End With

Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets

sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & sh.Range("A1").Value & ".pdf"

Next

MsgBox "Done"


End Sub
 
Upvote 0
Also has it created any files in the chosen location or none at all?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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