Save multiple PDF files from excel sheets with successive numbering in the name string

hassanleo1987

Board Regular
Joined
Apr 19, 2017
Messages
56
Hi,

I am creating multiple PDFs from a multi sheet workbook.
Previously, I used pre defined file names like this, which was working OK.

VBA Code:
File1 = ActiveWorkbook.Path & "\Part - 1.pdf"
File2 = ActiveWorkbook.Path & "\Part - 2.pdf"
File3 = ActiveWorkbook.Path & "\Part - 3.pdf"

But now I have added some conditions due to which some time one file is skipped making the series irregular.

I am looking for a dynamic file name were the string part "Part - " remains constant but the number changes successively based on last exported PDF file.
For example, if Part - 1.pdf has been created but Part - 2 is being skipped due to existing logic, the Part - 3 should be exported with the successive name as Part - 2.


I tried to do a loop for the filename but its not working and all parts are being printed as same file name, replacing the previous one.

VBA Code:
n = 1
    Do
        n = n + 1
        FNm = ActiveWorkbook.Path & "\Part - " & n & ".pdf"
    Loop Until Dir(FNm) = ""

For saving excelsheets as PDF I am using this:

VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FNm, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, from:=1, to:=1, OpenAfterPublish:=False

where FNm should be coming from the Do loop and change with every successive PDF export.

I would really appreciate some help here!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Additional Info, All PDF export files are being saved in the same directory as the excel file.
So the last file created can also be check in the same directory.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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