How do I Set Print Area Using VBA (With Examples)


You can use the following syntax in VBA to set the print area and display a print preview before actually printing a sheet:

Sub SetPrintArea()

    With Sheets("Sheet1")
     .PageSetup.PrintArea = Selection.Address
     .PrintPreview
    End With
    
End Sub

This particular macro will set the print area to be the currently selected cell range in the sheet called Sheet1 and then provide a print preview.

Note: If you want to print the selected range without previewing it, then replace .PrintPreview with .PrintOut in the macro.

The following example shows how to use this syntax in practice.

Example: How to Set Print Area Using VBA

Suppose we have the following sheet in Excel with some data about various basketball players:

We can create the following macro to specify a print area and display a print preview before actually printing a sheet:

Sub SetPrintArea()

    With Sheets("Sheet1")
     .PageSetup.PrintArea = Selection.Address
     .PrintPreview
    End With
    
End Sub

Suppose we then select the cell range A2:B7:

When we run this macro, the print area is automatically set to this selected cell range and the following print preview window appears:

This shows us exactly what the page will look like if we print the currently selected range of cells.

If we change the selected range of cells, then the print area will automatically change.

For example, suppose we instead select the range A1:B11:

If we run this macro again, the print area is automatically set to this selected cell range and the following print preview window appears:

The print preview now shows that we will print the range A1:B11 if we proceed with printing.

Note: You can find the complete documentation for the PrintArea property in VBA .

VBA: How to Print to PDF

x