Let’s start by introducing our dataset, which represents sales for a salesperson over 12 months for the years 2010 to 2015.
Private Sub CommandButton1_Click() Dim xSheet As Worksheet, y_File As FileDialog, x_Fldr As String, xYesorNo, I, xNum As Integer Dim Otlk_Obj As Object, x_Email As Object, x_Used_Range As Range, xArrSht As Variant Dim xAddress_PDF As String, y_Str As String, xRng_Exp As Range, xLast_Rng As Range xArrSht = sheetsArr(Me) For I = 0 To UBound(xArrSht) On Error Resume Next Set xSheet = Application.ActiveWorkbook.Worksheets(xArrSht(I)) If xSheet.Name <> xArrSht(I) Then MsgBox "Found no worksheet, exit operation:" & vbCrLf & vbCrLf & xArrSht(I), vbInformation, "PDF Fit to Page" Exit Sub End If Next Set y_File = Application.FileDialog(msoFileDialogFolderPicker) If y_File.Show = True Then x_Fldr = y_File.SelectedItems(1) Else MsgBox "Specify a folder to save PDF." & vbCrLf & vbCrLf & "Click OK to exit.", vbCritical, "Set a Folder" Exit Sub End If xYesorNo = MsgBox("If files with same name found, serial number will be added to the name" & vbCrLf & vbCrLf & "Press Yes to carry on, Press No to decline", _ vbYesNo + vbQuestion, "Duplicate File Found") If xYesorNo <> vbYes Then Exit Sub For I = 0 To UBound(xArrSht) Set xSheet = Application.ActiveWorkbook.Worksheets(xArrSht(I)) y_Str = x_Fldr & "\" & xSheet.Name & ".pdf" xNum = 1 While Not (Dir(y_Str, vbDirectory) = vbNullString) y_Str = x_Fldr & "\" & xSheet.Name & "_" & xNum & ".pdf" xNum = xNum + 1 Wend Set x_Used_Range = xSheet.UsedRange If Application.WorksheetFunction.CountA(x_Used_Range.Cells) <> 0 Then Set xLast_Rng = xSheet.Range("A" & xSheet.Rows.Count).End(xlUp) Set xRng_Exp = xSheet.Range(xLast_Rng.Offset(-26), xLast_Rng.Offset(, 7)) xRng_Exp.ExportAsFixedFormat Type:=xlTypePDF, Filename:=y_Str, Quality:=xlQualityStandard End If xArrSht(I) = y_Str Next Set Otlk_Obj = CreateObject("Outlook.Application") Set x_Email = Otlk_Obj.CreateItem(0) With x_Email .Display .To = "[email protected]" .cc = "[email protected]" .Subject = ". " For I = 0 To UBound(xArrSht) .Attachments.Add xArrSht(I) Next If .DisplayEmail = False Then End If End With End Sub Private Function sheetsArr(uF As UserForm) As Variant Dim c As MSForms.Control, strCBX As String, arrSh For Each c In uF.Controls If TypeOf c Is MSForms.CheckBox Then If c.Value = True Then strCBX = strCBX & "," & c.Caption End If Next sheetsArr = Split(Mid(strCBX, 2), ",") End Function
Code Breakdown:
Steps
Sub PDF_FitToPage_2() With ActiveSheet.PageSetup .Orientation = xlPortrait .PrintArea = "$B$2:$N$52" .PrintTitleRows = ActiveSheet.Rows(5).Address .Zoom = False .FitToPagesTall = False .FitToPagesWide = 1 End With ActiveSheet.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:="VBA ExportAsFixedFormat PDF Fit to Page", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=False, _ IgnorePrintAreas:=False, _ From:=1, _ To:=5, _ OpenAfterPublish:=True End Sub
Code Breakdown:
Below shows the printed PDF with fitted width on one page:
Steps
Sub PDF_FitToPage_3() Dim xSheet As Worksheet, Sheet_Name$, My_FilePath$, N& MyFilePath$ = "E:ExcelDemy" & _ Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) & Format(Date, "MM-DD-YYYY") With Application .ScreenUpdating = False .DisplayAlerts = False On Error Resume Next For N = 1 To Sheets.Count Sheets(N).Activate Sheet_Name = ActiveSheet.Name Cells.Copy Workbooks.Add (xlWBATWorksheet) With ActiveWorkbook With .ActiveSheet .Paste .Name = Sheet_Name [A1].Select .PageSetup.Orientation = xlLandscape .PageSetup.Zoom = False .PageSetup.FitToPagesWide = 1 End With .ExportAsFixedFormat Type:=xlTypePDF, Filename:=My_FilePath, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True .Close SaveChanges:=False End With .CutCopyMode = False Next End With Sheet1.Activate End Sub
Code Breakdown:
Excel has printed all the sheets of your workbook as separate PDF files, each fitted to one page.
You can download the practice workbook from here:
ExportAsFixedFormat PDF with Fit to Page.xlsm