Question posted 2013 · +10 upvotes
I have a reporting spreadsheet that grabs some data from a database and forms three sheets of summary report information. I want to run the spreadsheet automatically from the command line, and have it automatically save all three reporting sheets as a PDF file(s).
At first I thought I could have a VBA macro on the sheet do this by a series of “print as PDF”, but that requires an intermediary interactive dialog box to specify the output file name. Then I find that I can just save as pdf, and the macro can set the output file name. However this creates three separate files, and I have to then later put them together externally to merge them.
(It is odd that save as pdf only saves one sheet, where other save modes seem to save the entire workbook.)
Yes, there are tools for merging the three files later, but I want to know if there is some easy way to get Excel to save multiple sheets together as one pdf file.
I print now by a series of things like:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, ...
Could I do instead a single statement something like (pseudo-code):
ThisWorkbook.Sheets(1,2,3,4).ExportAsFixedFormat Type:=xlTypePDF, ...
Accepted answer +34 upvotes
Start by selecting the sheets you want to combine:
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:tempo.pdf", Quality:= xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Top vba Q&A (6)
- Difference between Visual Basic 6.0 and VBA +122 (2009)
- VBA – how to conditionally skip a for loop iteration +116 (2011)
- VBA: Test if string begins with a string? +53 (2013)
- html parsing of cricinfo scorecards +47 (2012)
- Code to loop through all records in MS Access +46 (2011)
- Access VBA | How to replace parts of a string with another string +44 (2011)
vba solutions on this site
— top 12%.