To set a print area in Excel with VBA, use the PageSetup.PrintArea property on a worksheet. The shortest working line is ActiveSheet.PageSetup.PrintArea = "$A$1:$D$20". Run it from a standard module (Insert > Module in the VBA editor) or from the Immediate Window (Ctrl+G). This locks the printed range so only those cells appear on paper or in PDF export, regardless of what else is on the sheet. You need Excel with macros enabled and a basic ability to open the VBA editor (Alt+F11). No add-ins are required.

What You'll Need

  • Excel for Windows or Mac (the VBA editor is included in both; the Mac editor is nearly identical but some dialog labels differ slightly).
  • A workbook saved as .xlsm (macro-enabled) if you want the code to persist.
  • Macro security set to allow macros, or the file opened from a trusted location.
  • The exact range address you want printed, e.g. A1:H30 or a named range.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (Mac). The editor opens in a separate window. Labels in the editor can differ slightly by Excel version and region, but the menu names below are standard.
  2. Insert a module. In the editor menu, choose Insert > Module. A blank code pane appears. If you want the code to run automatically, you can instead double-click the worksheet in the Project Explorer and paste into that sheet's code pane, but a standard module is simpler for manual runs.
  3. Write the basic statement. Paste this into the module:
    Sub SetPrintArea()
        ActiveSheet.PageSetup.PrintArea = "$A$1:$H$30"
    End Sub
    The dollar signs are absolute references and are optional; Excel accepts both A1:H30 and $A$1:$H$30. Use the address of the top-left and bottom-right cells of the block you want printed.
  4. Target a specific sheet rather than the active one. Replace ActiveSheet with a sheet reference to avoid printing the wrong tab:
    Worksheets("Report").PageSetup.PrintArea = "A1:H30"
    Use the tab name exactly as shown, including spaces inside the quotes.
  5. Run the macro. Place the cursor inside the Sub and press F5, or use Run > Run Sub/UserForm. If the Immediate Window is open (Ctrl+G), you can also type the single line directly and press Enter for a one-off test.
  6. Verify the result. Switch back to Excel and press Ctrl+P (or File > Print). The preview should show only the specified range. If it shows the whole sheet, the code did not run or the sheet reference was wrong.
  7. Clear the print area when needed. Set the property to an empty string:
    ActiveSheet.PageSetup.PrintArea = ""
    This removes the defined area so the whole used range prints again.
  8. Save the workbook as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx save strips the macro.

Common Mistakes

  • Using a sheet name that does not exist. Worksheets("Report") fails if the tab is named "Reports" or has a trailing space. Check the tab name character by character.
  • Forgetting quotes around the range. PrintArea = A1:H30 without quotes is treated as a variable and throws an error. The range must be a string.
  • Setting the area on the wrong sheet. ActiveSheet depends on which tab is selected when the macro runs. Prefer an explicit sheet reference.
  • Expecting the setting to apply to every sheet. PrintArea is per worksheet. Loop through sheets if you need it everywhere.
  • Saving as .xlsx. The macro disappears on save. Use .xlsm.
  • Assuming the print area controls what is exported to PDF automatically. It does when you use File > Export or Print to PDF, but some third-party tools ignore it.

Tips

  • Use a named range instead of a hard-coded address so the print area follows inserted rows: ActiveSheet.PageSetup.PrintArea = "MyRange".
  • To set the area for every worksheet in one pass, loop: For Each ws In Worksheets : ws.PageSetup.PrintArea = "A1:H30" : Next ws.
  • Pair the print area with PageSetup.Orientation and PageSetup.FitToPagesWide to avoid awkward page breaks.
  • Test on a copy of the workbook first; PageSetup changes are saved with the file and are easy to forget.
  • If you only need a one-time change, the Immediate Window is faster than creating a Sub.

Troubleshooting

If the macro runs without error but the print preview still shows the whole sheet, check three things in order. First, confirm you edited the sheet you are previewing, not a different tab. Second, confirm the range address is valid; a typo like A1:H silently sets nothing useful. Third, check whether the workbook is protected; a protected sheet blocks PageSetup changes and raises an error. Unprotect it (Review > Unprotect Sheet) or set the print area before protecting.

If you get a "Method 'PageSetup' of object '_Worksheet' failed" error, the usual cause is a protected sheet, a protected workbook structure, or a printer driver problem. Try selecting a different default printer in Windows settings, then rerun. On Mac, the same error often appears when no printer is configured; add one in System Settings > Printers & Scanners.

If nothing happens at all, macros may be disabled. Check the yellow security bar under the ribbon and enable content, or move the file to a trusted location. If the code is in a sheet module but you expected it to run from the module list, move it to a standard module.

Frequently Asked Questions

Can I set the print area for multiple non-adjacent ranges?

Yes. Separate the addresses with a comma inside the string, for example "A1:H10,A1:H10" style syntax with distinct blocks such as "A1:H10,D1:F20". Each block prints on its own page section.

Does setting the print area affect the screen view?

No. It only changes what is sent to the printer or PDF export. The worksheet looks the same on screen.

How do I set the print area for all sheets at once?

Loop through the Worksheets collection and assign the same PrintArea string to each sheet's PageSetup. Sheets with different layouts may need different ranges.

Why does the print area reset after I reopen the file?

Most often the workbook was saved as .xlsx, which drops macros, or the macro ran on a temporary copy. Save as .xlsm and confirm the code is still in the module.

Can I use a variable for the range address?

Yes. Build the string first, for example Dim r As String : r = "A1:H" & lastRow, then assign PageSetup.PrintArea = r. Make sure the final string is a valid range.

Is there a limit to how many cells the print area can contain?

The limit is the worksheet grid itself, not a special print-area cap. Very large areas simply span many printed pages.