Export Multiple Sheets to a Single PDF File – Excel VBA
文章推薦指數: 80 %
This tutorial will show you how to export multiple sheets of a workbook into a single PDF using Excel VBA. The code can be adapted. SkiptomaincontentSkiptoprimarysidebarSkiptofooterHome/VBA/ExportMultipleSheetstoaSinglePDFFile–ExcelVBAThistutorialwilldemonstratehowtoexportmultiplesheetsofaworkbookintoasinglePDFfileusingExcelVBA.Thecodecanbeseenbelowandadaptedforyourownuse.Ihavepreviouslywrittenatutorialonexportingallthesheetsofaworkbook toPDFusingVBA. Thismacrowillallowyoutobemorespecificastowhatsheetsareexported.ItwillalsoexporttoasinglePDFfile,ratherthanaseparatefileforeachsheet.SpecifyingtheSheetstobeExportedThekeytothemacroistoselectthesheetsthatyouwanttoexportfirst.Inthecodebelowthisisdonewiththefollowingline.Sheets(Array("Bradford","Kettering")).SelectThis referencesthesheetsusingtheirname.Youcouldalternativelyselectthesheetsusingtheirindexnumberlikebelow.Sheets(Array(2,4,5)).SelectAnotheralternativemaybethattheuserselectsthesheetsinExcel,andthenthemacroisrun.Inthiscommonscenarioofonlyexportingtheselectedsheets,youwouldnotneedthislineatallsoitcanberemovedfromthemacro.ExportingSpecificSheetstoaSinglePDFFile–VBACodeThisVBAcodecanbecopiedandpastedintoamodulefortheworkbook.Changethecodetoworkforyoursituation.SubExportAsPDF() DimFolderPathAsString FolderPath="C:\Users\Trainee1\Desktop\PDFs" MkDirFolderPath Sheets(Array("Bradford","Kettering")).Select ActiveSheet.ExportAsFixedFormatType:=xlTypePDF,Filename:=FolderPath&"\Sales",_ openafterpublish:=False,ignoreprintareas:=False MsgBox"AllPDF'shavebeensuccessfullyexported." EndSubIfyouwerehopingtoexportsheetsfrommultipleworkbookssavedsomewhereinyournetwork,thencheckoutthistutorialonloopingthroughthefilesofafolderusingVBA.BycombiningthecodefromthetwomacrosyoucouldexportmanyPDF’swithoneclickofabutton.MoreawesomeExcelVBATutorialsLoopThroughtheSubfoldersofaDirectoryusingFileSystemObjectAutomaticallyCreateaTableofContentsinExcelUsingtheOffsetPropertyinVBARelatedPosts:WhatYouNeedtoKnowAboutDynamicArrayFormulasinExcelImportMultipleExcelFileswithMultipleSheetsinExcelExcelFormulatoFindtheCellAddressofaValueExcelFILTERFunction–TheBestFunctioninExcelReaderInteractionsCommentsThisisgreat–Thankyousomuchforshowingmethis.Ihavethisexactsituation–butIneedtoprintChartsinseparateexcelsheetsallintoonepdf.Iwouldliketohaveeachchartfitto1pagetalland1pagewideregardlessofwherethechartisineachsheetandregardlessofit’ssize.Doyouknowhowtodothis?I’mnewtoallprogrammingsoanythoughtsappreciated.ThanksagainReplyThanksforthisAlan.Thisisterrific.Ihavetocreatesheetsinaworkbookformyboss.WiththiscodeIcanprintaPDFdocumentwithallthesheetsonit.ManythanksPeterReplyYourwelcomePeter,thanks.ReplyHello.Iamtryingouthismacroanditallworkshowevericouldlikethefilenametobelinkedtacallintheexcelfilee.g.itshouldlinktocellC6insteadof“Sales”likeyourexampleabove.Isthereanywayofdoingthis?ReplyHiGeorgia,Sure,youcanadaptthatbitofcodeasfollows.Filename:=FolderPath&“\”&Worksheets(“Kettering”).Range(“C6”)AlanReplySuperCanyouexportonlyonepage(exampleallselectedsheetssecondpages)toonlyoneoutputPDF?ReplyI’msurethiswouldbepossibleusingtheprintsettings.ItisnotcodethatIhavetohandthough.ReplyI’mgettingaRuntimeerror‘9’SubscriptoutofrangefortheThisWorkbook.Sheets(Array(“Sum_instructed”,“Sum_pending”)).SelectReplyJustcheckthatthesheetnamesarecorrect,becausethattypeoferrorindicatesthatitcannotfindthosesheets.Also,thatthemacrointhesameworkbookfilethatyouarerunningiton.ThisWorkbookreferstotheworkbookthatthemacroresidesin.ReplyHello,howtochangetheorderinapdffile,howworksheetsareissued?Ineedthe“introduction”toalwaysbebefore“Sheet1”,butIalwaysgetthefirstpageas“Sheet1”inthecontentofthepdffile.Itriedbothwithoutluck:Spreadsheets(array(“introduction”,“page1”)).SelectSpreadsheets(array(“Sheet1”,“intro”)).SelectHowtochangeprintoutsequence?ReplyHelloThankyouforaverygreatvideo.Iwasjustwondering,isitpossibletogetthepossibilitytotypethenameofthefile,insteadofitiswritteninthecode….??“Filename:=FolderPath&“\Sales”BestregardsAllanReplySure,thenamecouldbepickedupfromarangeonthesheetormaybehaveaninputboxpromptyouifyouprefer.ReplyHiAllan,Couldyoupleasemodifythecodetohaveitpopupforthelocationtosavethefile?Thanks.ReplyThiscanbedoneusingapplication.FileDialog(msoFileDialogFolderPicker).IshowanexampleofusingthisinmyVBAcourse–http://bit.ly/37XSKfZReplyLeaveaReplyCancelreplyYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Comment*Name*Email*WebsiteSavemyname,email,andwebsiteinthisbrowserforthenexttimeIcomment. ΔPrimarySidebarPopularPostsExcelFixturesandLeagueTableGeneratorExcelIFFunctionContainsText–APartialMatchinaCell5ReasonsWhyyourExcelFormulaisNotCalculatingExcelFormulatoDisplaytheSheetNameinaCell4AlternativestoNestedIFFormulasHowtoCreateaReverseFINDFormulaCheckifaValueisanEvenNumberHowtoHyperlinktoaHiddenWorksheetinExcelIFFunctioninPowerQueryIncludingNestedIFSConditionalFormattingMultipleColumns–3ExamplesreportthisadRecentPostsSEQUENCEFunctioninExcelSortbyDropDownListinExcelAdvancedSUMFunctionExamples–ThePowerofSUMDisplayNegativeTimeinExcelLookupMultipleValuesinExcelreportthisadreportthisadFooterContact[email protected]UsefulLinksCourseTopicsChartsCoursesDashboardsDownloadsExcelTipsFormulasMailMergeOfficePivotTablesPowerBIPowerPivotPowerQueryVBAJoinOurMailingListxx
延伸文章資訊
- 1Excel Macro to Save Sheets As PDF - Contextures
Export the Active Sheet as PDF File
- 2Print Multiple Excel Sheets to Single PDF File with VBA (6 ...
- 3Save Multiple Sheets as PDF in Excel & Google Sheets
In the Print window, (1) under Export, select Workbook. (2) Tick Sheet 1 and Sheet 2 for the sele...
- 4How to save selection or entire workbook as PDF in Excel?
- 5Excel VBA: to export selected sheets to a single ... - Super User
What errors are you getting when running this macro? · I do not get any errors, but the pdf-file ...