Forum Discussion
VBA Script to Print Selected Content in Excel
Replace the following line of code:
vba code
totalPages = WorksheetFunction.Ceiling(selectedRange.Height / ActiveSheet.PageSetup.PaperHeight, 1)
With this updated calculation:
vba code
totalPages = Application.Ceiling(selectedRange.Height / ActiveSheet.PageSetup.PaperHeight, 1)
By using the Application.Ceiling method instead of WorksheetFunction.Ceiling, you can perform the same rounding up operation to calculate the number of pages needed.
Same error occurs at line of code "totalPages = Application.Ceiling(selectedRange.Height / ActiveSheet.pageSetup.PaperHeight, 1)". Not sure what else we can do to solve the compatibility issue. Really irritated....
- NikolinoDEJun 12, 2023Platinum ContributorIf possible, please insert the file (without sensitive data).Additional information such as Excel version, operating system, storage medium (hard drive, OneDrive, Sharepoint, USB, etc.) would be beneficial.Thank you for your patience and understanding
- lj4028aimJun 12, 2023Copper Contributor
Sorry, couldn't load the origin file, but here is the screen shot to represent the file. The selected range means from cell A1 to B37, but the range is really depending on the user's selection which means it will be a dynamic range. The version of Excel if Microsoft 365 (Enterprise) and OS is Windows 10 (10.0.19045) Enterprise. Physical memory is 16G. Hopefully this helps for debugging. Thanks.
- NikolinoDEJun 13, 2023Platinum Contributor
The setup of the print settings and margin settings are correct.
Here is an example of how you can modify if statement to check if the selected range fits on one page:
Sub PrintWorkbookToPDF() ' Store the current selection range Dim selectedRange As Range Set selectedRange = Selection ' Set print settings With ActiveSheet.PageSetup .PaperSize = xlPaperLetter ' Set paper size to 8.5x11 inches .PrintQuality = 2400 ' Set print quality to 2400 dpi .CenterHorizontally = True ' Center horizontally on page .CenterVertically = True ' Center vertically on page .LeftHeader = "" ' Set header to blank .RightHeader = "" ' Remove right header .LeftFooter = "" ' Remove left footer .RightFooter = "" ' Remove right footer .Orientation = xlLandscape ' Set orientation to landscape ' Set margin settings to normal .TopMargin = Application.InchesToPoints(0.75) .BottomMargin = Application.InchesToPoints(0.75) .LeftMargin = Application.InchesToPoints(0.75) .RightMargin = Application.InchesToPoints(0.75) .HeaderMargin = Application.InchesToPoints(0.5) .FooterMargin = Application.InchesToPoints(0.5) ' Set the print area to the selected range .PrintArea = selectedRange.Address ' Check if the selected range fits on one page Dim printableHeight As Double Dim printableWidth As Double printableHeight = .PaperHeight - .TopMargin - .BottomMargin printableWidth = .PaperWidth - .LeftMargin - .RightMargin If selectedRange.Height <= printableHeight And selectedRange.Width <= printableWidth Then ' If the selected range fits on one page, use the default settings .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = 1 Else ' If the selected range doesn't fit on one page, adjust the content's scale to fit on one page .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False ' Remove this line to let the content scale vertically as necessary End If End With ' Prompt the user to select the folder and enter the file name With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Select Folder to Save PDF" If .Show = -1 Then ' Get the selected folder path folderPath = .SelectedItems(1) ' Prompt the user to enter the file name Filename = InputBox("Enter the File Name", "Save PDF") ' Save the workbook as PDF filePath = folderPath & "\" & Filename & ".pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filePath, Quality:=xlQualityStandard MsgBox "PDF file saved successfully." Else MsgBox "No folder selected. PDF file not saved." End If End With End SubI hope that works, otherwise I am at my wits end.