Set a Dynamic Print Area for a Variable Range in Excel VBA
Question details
Set an Excel print area that adjusts to a variable data range without selecting individual cells.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA macro needs to define one contiguous print area for data whose dimensions can change.
- Observed behavior
- The desired print area is a single rectangular range that reflects the current data instead of multiple noncontiguous areas.
Identify the worksheet name and decide whether its used range or a specific rectangular data block should be printed. Save the workbook in a macro-enabled format such as .xlsm and ensure macros are enabled if you intend to run the code.
Set the Print Area to the Worksheet's Used Range
Use this approach when the worksheet's used range matches the block you want to print.
UsedRange returns a contiguous rectangle, so there is no need to select cells or build a Union. It can include previously formatted or edited cells outside the visible data.
In Excel, open Developer > Visual Basic, then choose Insert > Module.
Paste the following code into the module and replace Sheet1 with your worksheet name: Sub SetUsedRangePrintArea() With Worksheets("Sheet1") .PageSetup.PrintArea = .UsedRange.Address End With End Sub
Run SetUsedRangePrintArea from Developer > Macros, then open File > Print to inspect the preview. Run the macro again after the data range changes.

Set the Print Area to a Calculated Rectangular Range
Use this approach when your data starts at A1, column A identifies the last data row, and row 1 identifies the last data column.
Work with Spreadsheet Print Areas in WPS Office
WPS Office is a free, lightweight alternative for opening, editing, and printing spreadsheets. Its familiar interface and Excel-format support make it easy to move everyday spreadsheet work between applications. It does not fix Excel's VBA behavior, so check macro compatibility before migrating a macro-dependent workbook.

Frequently Asked Questions
Why does selecting nonempty cells create so many printed pages?
Selecting individual nonempty cells can create a noncontiguous range. Excel may print separate areas on separate pages, so assign one rectangular address to PageSetup.PrintArea instead.
Does the print area update automatically when data changes?
Not with these macros alone. Run the macro again through Developer > Macros after the data changes, or arrange for it to run before printing.
Why does UsedRange include blank-looking cells?
Previously edited or formatted cells can extend the used range beyond visible data. Check those cells or define a specific rectangular range instead.
How do I remove the print area?
In Excel, select the worksheet and choose Page Layout > Print Area > Clear Print Area. In VBA, you can set the worksheet's PageSetup.PrintArea property to an empty string.
Do I need arguments inside the macro's parentheses?
No. A Sub procedure can have no arguments, so a declaration such as Sub SetUsedRangePrintArea() is valid.




