Converting Individual Excel Sheets as Individual PDF Files - excel

I am just trying to use VBA to create create/save individual excel sheets as individual PDF files.
I came across this online:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next wks
End With
End Sub
However, I'm getting the following error:
Run-time error '5':
Invalid procedure call or argument
For someone not 100% familiar with VBA, what's invalid?
Thanks

As I said in my comment, your code runs fine (Win-10, Excel-365). I first:
Saved the .xlsm file to insure Spath existsSelected an area on each worksheet to insure it will be PDF'ed correctly

Related

Is there a reason my vba macro to transfer an excel sheet to a pdf doesn't show everything?

This is the code for a button I have on my userform to change the active sheet in my workbook to a pdf. I'm wondering why not everything in my excel sheet is showing. The first 6-7 pages of my excel sheet show perfectly fine, and it's one or two sections at the end of my sheet that show portions, or nothing at all
Private Sub CommandButton3_Click()
Dim strFile As String
Dim invoiceRng As Range
Dim pdfFile As String
'Setting range to be printed
Set invoiceRng = Range("A1:D850")
'setting file name with a time stamp.
strFile = "Checklist" & "_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"
'setting the name. The resultent pdf will be saved where the main file exists.
pdfFile = ThisWorkbook.Path & Application.PathSeparator & strFile
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
End Sub

Create a pdf for each selected Excel sheet

I'm trying to create separate PDFs for each sheet in a selection of sheets, with a name determined by the sheet name and the contents of one cell.
The code is as follows:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim sh As Object
Dim InvDate As String
With ActiveWorkbook
sPath = .Path & "\"
For Each sh In ActiveWindow.SelectedSheets
InvDate = Format(Range("G9"), "dd-mm-yy")
sFile = sh.Name & " - Invoice - " & InvDate & ".pdf"
sh.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next sh
End With
End Sub
Separate PDFs are created and named correctly, however, the PDF contains all the selected sheets instead of one.
With my experience, i've seen that the ExportAsFixedFormat always exports all selected sheets.
You can come around this by saying sh.Select after your For Each, and then it should works as you describe in the OP.
Edited code:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim sh As Object
Dim InvDate As String
With ActiveWorkbook
sPath = .Path & "\"
For Each sh In ActiveWindow.SelectedSheets
sh.Select '<----- New LINE
InvDate = Format(Range("G9"), "dd-mm-yy")
sFile = sh.Name & " - Invoice - " & InvDate & ".pdf"
sh.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=False
Next sh
End With
End Sub
Hope this helps you achive your goal. :)

Add exclusion to Excel macros

So I'm using this macro to save individual excel worksheets as their own PDF when run:
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next wks
End With
End Sub
I would just like to add to it to not start from a certain worksheet or to exclude certain worksheets. Any clue how?
You can use Select Case to specify sheetnames to ignore, as shown. Simply replace the "IgnoreSheet1", "IgnoreSheet2" with the actual sheet names you want skipped. It's just a comma delimited list, so add as many as you want.
Sub SaveWorksheetsAsPDFs()
Dim sFile As String
Dim sPath As String
Dim wks As Worksheet
With ActiveWorkbook
sPath = .Path & "\"
For Each wks In .Worksheets
Select Case wks.Name
Case "IgnoreSheet1", "IgnoreSheet2" 'Do nothing
Case Else
'Code here to run on the other sheets
sFile = wks.Name & ".pdf"
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=sPath & sFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Select
Next wks
End With
End Sub

Excel: Saving Selected Worksheets into PDF file

Although this questions was already asked I did not find a real answer for it.
I need to programmatically select worksheets in a workbook and save them to PDF file WITHOUT creating temporary workbook and copying selected worksheets to it.
Alternatively how do create workbook without displaying it - that is, in memory and then on HD? This is again may help to solve the first question...
Try this:
Sub SaveToPdf()
Dim ws As Worksheet
Dim sfName As String
For Each ws In Worksheets
sfName = ws.Name
sfName = "C:\Misc\" & sfName & ".pdf"
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sfName, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next ws
End Sub
You'd need to make sure your print areas on each sheet were set first I think

Excel VBA to Save Selected Sheets in Combination as PDFs

I have a workbook with many worksheets. I would like to save as two-page PDFs, where the first page is Worksheet 1, and the second page is Worksheets 2-x. My code currently only allows me to save individual PDFs for each worksheet in the workbook. I am wondering what to add to it to make it do this. Can anyone share some advice?
Thanks!
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
You are enumerating through the worksheets and doing your save inside that loop. That is why you are getting one PDF per worksheet. Try using just workbook instead of ActiveWorkbook.Worksheets.
Gah. It was staring me in the face the whole time. I amended the code to include a selection, and named the second worksheet ws.Name. Final script looks like this:
Option Explicit
Sub createPDFfiles()
Dim ws As Worksheet
Dim Fname As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next
Fname = "C:\Folder\" & ws.Name & "Report" & Format(Date, "yyyy-mm-dd") & ".pdf"
Sheets(Array("Sheet1", ws.Name)).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False
Next ws
End Sub
Thanks for your help everyone!

Resources