Excel VBA: Print Preview for Selected Range (5 Examples)

In this article we will demonstrate some methods for printing previews for a selected range of cells in Excel by using Microsoft Visual Basic for Application (VBA). We’ll use the following dataset containing information about the sales of some employees to illustrate our methods.

excel vba print preview selected range


Excel VBA to Show Print Preview for Selected Range (Quick View)

Sub Print_Range_Method()
Sheets("Range Method").Select
Range("B4:F9").Select
ActiveSheet.PageSetup.PrintArea = "B4:F9"
ActiveWindow.SelectedSheets.PrintOut From:=1, _
To:=1, Copies:=1, Collate:=True
End Sub

excel vba print preview selected range


Example 1 – Using VBA PrintOut Method

From our dataset, let’s print preview a selected range using a simple VBA code.

Step 1:

  • Open a Module by going to Developer → Visual Basic.

Apply Excel VBA PrintOut Method to Print Preview Selected Range

A window named Microsoft Visual Basic for Applications – Print Preview Selected Range will pop up.

  • In that window, go to Insert → Module.

Step 2:

The Print Preview Selected Range module pops up.

  • Enter the following VBA code in the module:
Sub Print_Preview_Selected_Range()
Range("B4:F15").PrintOut
End Sub

Apply Excel VBA PrintOut Method to Print Preview Selected Range

  • Run the code by going to Run → Run Sub/UserForm.

Step 3:

The Save Print Output As window will appear.

  • Select “PDF Document” as the Save as type.
  • Give the PDF file a name, here PrintOut.
  • Click Save.

Apply Excel VBA PrintOut Method to Print Preview Selected Range

  • Navigate to the save location and open the file.

You will see the selected range.

Read More: Excel VBA: Print Range of Cells


Example 2 – Defining the Excel Sheet

In this method, we will define the sheet name to print preview.

Step 1:

  • As per Example 1, insert a new module and enter the following VBA code:
Sub Define_Sheet()
Sheets("Define Sheet").Range("B4:F10").PrintOut
End Sub

Defining the Excel Sheet to Print Preview Selected Range by Excel VBA

  • Run the code by clicking Run → Run Sub/UserForm.

Step 2:

  • The Save Print Output As window will appear.
  • Save as a PDF file named Define Sheet.
  • Click Save.

Defining the Excel Sheet to Print Preview Selected Range by Excel VBA

  • Locate and open the saved file.

Defining the Excel Sheet to Print Preview Selected Range by Excel VBA

To view the print preview of this selected range of cells, press CTRL + P and give the necessary commands.

Read More: Excel VBA: Set Print Area for Multiple Ranges


Example 3 – Using Print Preview Selected Range to Print

Another way to print a range of cells using VBA is to select the range that we want to print preview.

Step 1:

  • Select a range of cells from our dataset, for example B2 to F9.

Use Print Preview Selected Range to Print by Excel VBA

  • As per Example 1, insert a new module and enter the below VBA code in it:
Sub Selection_of_Print_Preview()
Range("B2:F9").PrintOut
End Sub

  • Run the code by clicking Run → Run Sub/UserForm.

Use Print Preview Selected Range to Print by Excel VBA

Step 2:

  • In the Save window that appears, save the file as type “PDF Document” with the name Selection.
  • Click Save.

  • Locate and open the saved file.

Use Print Preview Selected Range to Print by Excel VBA

  • View the print preview of this selected range of cells by pressing CTRL + P and giving the necessary commands.

Example 4 – Defining the Selected Range in the VBA Code

We can also print a range of cells by VBA using the Range Method. Let’s print the information of the first four entries.

Step 1:

  • As per Example 1, insert a new module and enter the below VBA code in it:
Sub Print_Range_Method()
Sheets("Range Method").Select
Range("B4:F9").Select
ActiveSheet.PageSetup.PrintArea = "B4:F9"
ActiveWindow.SelectedSheets.PrintOut From:=1, _
To:=1, Copies:=1, Collate:=True
End Sub

Define Selected Range in the VBA Code to Print Preview Selected Range

  • Run the code by clicking Run → Run Sub/UserForm.

Step 2:

  • In the Save window that appears, save the file as type “PDF Document” with the name Range Method.
  • Click Save.

Define Selected Range in the VBA Code to Print Preview Selected Range

  • Open the saved file to see the selected range.

Define Selected Range in the VBA Code to Print Preview Selected Range

  • View the print preview of this selected range of cells by pressing CTRL + P and giving the necessary commands.

Example 5 – Using VBA With Statement

Step 1:

  • As per Example 1, insert a new module and enter the below VBA code in it:
Sub Print_Preview_With_Statement()
With Sheets("Statement")
.PageSetup.PrintArea = "B4:F15"
.PrintOut
End With
End Sub

Printing Preview Selected Range by Excel VBA With Statement

  • Run the code by clicking Run → Run Sub/UserForm.

Step 2:

  • In the Save window, save the file as type “PDF Document” with the name Statement.
  • Click Save.

Printing Preview Selected Range by Excel VBA With Statement

  • Open the saved file to view the selected range.

  • View the print preview of this selected range of cells by pressing CTRL + P and giving the necessary commands.

Things to Remember

You can also pop up the Microsoft Visual Basic for Applications window by using the keyboard shortcut Alt + F11.

If the Developer tab is not visible in your ribbon, enable it by going to File → Option → Customize Ribbon.


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Md. Abdur Rahim Rasel
Md. Abdur Rahim Rasel

MD. ABDUR RAHIM is a marine engineer proficient in Excel and passionate about programming with VBA. He views programming as an efficient means to save time while managing data, handling files, and engaging with the internet. His interests extend to Rhino3D, Maxsurf C++, AutoCAD, Deep Neural Networks, and Machine Learning, reflecting his versatile skill set. He earned a B.Sc in Naval Architecture & Marine Engineering from BUET, and now he has become a content developer, creating technical content... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo