Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Set Print Area with a Variable Range

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To set a print area with a variable range in Excel VBA, use the PageSetup.PrintArea property and build the range address as a string from variables (for example, the last used row and column). The shortest working pattern is: ActiveSheet.PageSetup.PrintArea = Range(Cells(1, 1), Cells(lastRow, lastCol)).Address. That assigns the print area dynamically each time the macro runs, so it adapts as rows and columns are added or removed.

What You'll Need

  • Excel with macros enabled (the .xlsm file format if you save the workbook).
  • Basic familiarity with the VBA editor: press Alt+F11 on Windows, or use the Developer tab on Mac.
  • A worksheet where the data size changes, which is exactly when a fixed print area stops working.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or go to Developer > Visual Basic (Mac). If the Developer tab is hidden, enable it in Excel options. Labels and menu wording can differ slightly by Excel version, platform and region.
  2. Insert a standard module. In the editor, choose Insert > Module. Keeping the code in a module makes it easy to run from the Macros dialog or assign to a button.
  3. Declare variables for the last row and column. Use Long variables and find the last used row and column with Cells(Rows.Count, 1).End(xlUp).Row and Cells(1, Columns.Count).End(xlToLeft).Column. Adjust the column or row reference if your data does not start in column A or row 1.
  4. Build the range and assign the print area. Use Range(Cells(1, 1), Cells(lastRow, lastCol)).Address to produce a valid address string, then assign it to ActiveSheet.PageSetup.PrintArea. For a specific sheet, replace ActiveSheet with the sheet's code name or a Worksheets("Sheet1") reference.
  5. Handle the empty-sheet case. If the sheet has no data, the last-row calculation can return 1 and produce a misleading print area. Add a check such as If Application.WorksheetFunction.CountA(Cells) = 0 Then Exit Sub before setting the print area.
  6. Clear the old print area first if needed. Setting PrintArea to an empty string clears it: ActiveSheet.PageSetup.PrintArea = "". This is useful when the new range might be smaller than the old one.
  7. Run the macro. Press F5 in the editor, or go back to Excel and use Developer > Macros. Check the result with File > Print or Ctrl+P; the preview should show only the intended range.
  8. Save the workbook as .xlsm. A normal .xlsx save strips the macro. Use File > Save As and choose Excel Macro-Enabled Workbook.

Common Mistakes

  • Using UsedRange blindly. UsedRange can include formatting far beyond the real data, producing a huge print area. Calculating the last row and column from actual content is more reliable.
  • Forgetting the sheet reference. ActiveSheet depends on which sheet is active. If the macro runs from a button on another sheet, the print area may land on the wrong one.
  • Passing a Range object instead of a string. PrintArea expects an address string, so use .Address or build a string like "A1:D" & lastRow.
  • Ignoring hidden rows or columns. A print area includes hidden cells; if you want them excluded, unhide or filter first, or set the area to visible ranges only.
  • Not clearing the previous area. The old print area can persist if the new assignment fails or is skipped, so clear it explicitly when the range may shrink.

Tips

  • Store the last row and column in named variables so the code is readable and easy to adjust.
  • Use With blocks to set PrintArea, orientation and fit-to-page together, which keeps related page setup in one place.
  • Test on a copy of the workbook first; page setup changes are saved with the file.
  • If the print area should cover multiple non-adjacent blocks, join their addresses with a comma, for example "A1:C10,E1:G10".
  • For recurring use, assign the macro to a button on the sheet so users can refresh the print area without opening the editor.

Troubleshooting

  • The macro errors with "Unable to set the PrintArea property". Check that the address string is valid and that the sheet is not protected. Unprotect the sheet or allow page setup changes before running the macro.
  • The print area is wrong or too large. Verify the last-row and last-column calculations against the actual data. A stray value far down the sheet can inflate the range.
  • Nothing happens when the macro runs. Confirm macros are enabled and that the code is in a standard module, not a sheet or workbook module, unless you intended event code.
  • The print preview still shows the old area. Clear the print area first, then reassign it, and refresh the preview.
  • The workbook opens with a security warning. This is normal for macro-enabled files; enable content only for workbooks you trust.

Frequently Asked Questions

Can I set the print area to a variable number of rows only?

Yes. Build the address with a fixed column and a variable row, for example "A1:D" & lastRow, and assign it to PrintArea.

Does setting PrintArea affect other sheets?

No. PrintArea applies to the sheet you reference, so specify the sheet explicitly if the active sheet may change.

How do I remove a print area with VBA?

Assign an empty string: ActiveSheet.PageSetup.PrintArea = "". This clears the saved print area for that sheet.

Can the print area include multiple separate ranges?

Yes. Join the addresses with a comma, such as "A1:C10,E1:G10", and assign the combined string.

Why does my macro fail on a protected sheet?

Page setup changes are blocked when the sheet is protected. Unprotect the sheet first, or allow page setup in the protection settings, then run the macro.

Will the print area update automatically when data changes?

Not by itself. The macro sets the area at the moment it runs, so rerun it after the data size changes, or call it from a worksheet event if appropriate.