VBA & Macro Problems

Set a Dynamic Print Area for a Variable Range in Excel VBA

Huma Ashraf ChHuma Ashraf Ch Nov 18, 2026 868 views

Question details

Set an Excel print area that adjusts to a variable data range without selecting individual cells.

Set a Dynamic Print Area for a Variable Range in Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open Developer > Visual Basic, then choose Insert > Module.

2
Add the macro

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

3
Run and check the macro

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 the Worksheet's Used Range
Check the used range: If the preview includes unwanted blank rows or columns, check for formatting or previous edits outside the data, or use the rectangular-range method instead.
Free Microsoft Office alternative

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.

Set and preview spreadsheet print areas for everyday printingOpen and edit common Microsoft Excel formats, including XLSXUse a familiar spreadsheet interface for easier migrationWork with a free, lightweight office suite
microsoft office alternative - wps office

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.