save sheets as PDF

Arie Bos

Board Regular
Joined
Mar 25, 2016
Messages
224
Office Version
  1. 365
Platform
  1. Windows
I use the following code to save all sheets with names starting with "LP" as separate pdf files:

Code:
Sub Save_all_Reports()
Dim Fname As String
Dim Sht As Worksheet
    For Each Sht In ActiveWorkbook.Sheets
        If UCase(Left(Sht.Name, 2)) = "LP" Then 'Only include sheets beginning LP
    
        Fname = "C:\Users\Arie\B-II\Monthly Reports\17_10\" & "B-II" & " [" & Sheets("START").Range("SelectedMonth") & "] " & Sht.Range("Z11") & ". " & Sht.Range("i4") & ".pdf"
        Sht.ExportAsFixedFormat 0, Fname
    
        End If
    Next Sht
End Sub

2 questions:
1. The code halts after successfully saving the first report as "B-II [17_10] LP01. John Doe" (where 17_10 comes out of cell name 'SelectedMonth', LP01 comes of cell Z11 and John Doe come out of cell I4). The error which pops up is:"run time error 1004: Document is not saved. The document may be open, or an error may have been encountered when saving".
2. In the path definition the pdf's must be saved into the correct month folder. So every month I create a folder under with the new month name so, 17_10, 17_11 etc. Then I change this month number in the path above (Fname = ...). Of course this is not much work, but for elegancy, can this be done by the macro by reading the month number out of "SelectedMonth" ?

Thank you Excelleers!

Arie
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

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