
Note: You can find the complete documentation for the PrintPreview method in VBA here. strfile Range('D48') & ' - ' & dt & '.pdf' setting the fulli qualified name. This shows us exactly what the page will look like if we print only the selected area. VBA Code: ub PrintSelectionToPDF() Dim PrivateAttestationRng As Range Dim strfile As String Dim dt As String dt Format(Now(), 'ddmmyyyyhhmmss') Setting range to be printed Set PrivateAttestationRng Range('B2:E137') setting file name with a time stamp. We can create the following macro to perform a print preview to see what this selected area will look like on a page before we actually print it: Sub UsePrintPreview() Suppose we select the cell range A1:C4 and only want to print this selected area. Example 2: Print Preview for Selected Area Step 2: Name the Macro In the present case, we have kept the name of our Macros as Macro1 which is a default name provided. As soon as we put this option, we get the below mentioned screen.

Step 1: Create a Macro as follows Select the Data ViewMacros Record Macro. This shows us exactly what the page will look like if we print the entire currently active sheet. Follow the below steps to use the Print function in Excel VBA.
#Vba print selection code#
Steps: Type the following code in the VBA Module. Suppose you want to print only the Name, Age, and Occupation of these people. When we run this macro, the following print preview window appears: Selecting Range of Cells to Print by Excel VBA Another way to print a range of cells by VBA is to select the range that we prefer to print. We can create the following macro to perform a print preview to see what the printed page will look like before we actually print it: Sub UsePrintPreview() Suppose we would like to print the entire active sheet.
#Vba print selection how to#
The following examples show how to use each method with the following active sheet in Excel:Įxample 1: Print Preview for Entire Sheet This particular macro will provide a print preview for only the currently selected area of the sheet. Method 2: Print Preview for Selected Area Sub UsePrintPreview() This particular macro will provide a print preview for the entire currently active sheet. Method 1: Print Preview for Entire Sheet Sub UsePrintPreview() PrivateAttestationRng.You can use the following methods in VBA to display a print preview before actually printing a sheet: StrFile = ThisWorkbook.Path & "\Private Attestation - " &.

With ThisWorkbook.Worksheets(ThisWorkbook.Worksheets("MASTER_EN").Range("G17").Value) Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False PrivateAttestationRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFile, _
#Vba print selection pdf#
'Full PDF file name with timestamp, in same folder as workbook 'Range in Master C17-specified sheet to be saved With ThisWorkbook.Worksheets(ThisWorkbook.Worksheets("MASTER_EN").Range("C17").Value) Is this something you could still help with ? This option will be available in Menu ->Page Layout -> Print Area. I have now inserted a button in each sheet, but Ideally, I would like to be able to print a sheet based on a value of eg cell C17 in my master tab. Set Print area Choose which cells have to appear in the paper after a print. I have like about 15 sheets, all identical, apart from the language. Example : All of these might be confusing. Now to confirm your selected cells Go to View > Page Break preview. After selecting your preferred option, you can go ahead and print the worksheet. Go to Page Layout > Print Area > Set Print Area. PrivateAttestationRng.ExportAsFixedFormat _ You can use VBA in Excel to print comments. PrintArea PrintRange.Address Application.PrintCommunication False. Strfile = ThisWorkbook.Path & "/Private Attestation - " & strfile This code worked for me: Sub Test () Dim PrintRange As Range Set PrintRange Selection With 'The Parent of Selection is the sheet. The resultent pdf will be saved where the main file exists. Set PrivateAttestationRng = Range("B2:E137")
