VBA & Macro Problems

Print an Excel VBA Used Data Range One Page Wide

Huma Ashraf ChHuma Ashraf Ch Nov 12, 2026 869 views

Question details

Create a VBA macro that prints the range from A1 to column D’s last populated row, using landscape orientation and one-page-wide scaling.

Print an Excel VBA Used Data Range One Page Wide
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
The user needs to print a variable-size worksheet data area without manually setting the print range each time.
Observed behavior
The desired result is for A1:D[last populated row in column D] to print in landscape orientation and fit across one page wide.
Before you start

Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm) and enable macros when opening it in the desktop app. Confirm that column D is populated for every row that should be included in the print area.

Solution 1Recommended

Use VBA to set the print area and fit it to one page wide

Use this macro when the printable data always begins at A1 and ends in column D, but the number of populated rows changes.

The macro finds the last nonblank cell in column D, assigns A1 through that row as the print area, switches the sheet to landscape orientation, and prints it one page wide. The output may span multiple pages vertically.

1
Open the Visual Basic Editor

In Excel, press Alt+F11, then select Insert > Module to create a standard VBA module.

2
Paste the print macro

Paste the following code into the module: Sub prt() Dim LR As Long Dim ws As Worksheet Dim rngPrint As Range Set ws = ActiveSheet LR = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row Set rngPrint = ws.Range("A1:D" & LR) With ws.PageSetup .PrintArea = rngPrint.Address .Orientation = xlLandscape .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False End With rngPrint.PrintOut End Sub

3
Run the macro

Return to the worksheet, press Alt+F8, select prt, and click Run. Excel sends the defined range to the selected printer.

4
Review the page layout if needed

To preview the result before printing, replace rngPrint.PrintOut with rngPrint.PrintPreview, or open File > Print after running the macro.

Use VBA to set the print area and fit it to one page wide
How the last row is detected: The macro uses the last nonblank cell in column D. If column D can be blank while other columns contain required data, change "D" in the LR line to a consistently populated column.
Print dynamic worksheet ranges with macros

Print a Dynamic Data Range in WPS Spreadsheets

WPS Spreadsheets provides familiar worksheet printing controls and macro tools for compatible Excel workbooks. Open the macro-enabled workbook, confirm the print area and landscape scaling, then run or adapt the macro for the active sheet.

  1. 1. Open the workbook in WPS Spreadsheets: Launch WPS Office, choose Spreadsheets, and open the XLSM or XLSX workbook containing the data.
  2. 2. Check the print range: Select A1 through the final row in column D, then use Page Layout > Print Area > Set Print Area if you want to verify the target range manually.
  3. 3. Set landscape and one-page-wide scaling: Go to Page Layout and choose Orientation > Landscape, then set the page scaling option to fit the sheet to one page wide.
  4. 4. Run or add the macro: Open the Developer or Macro tools available in your WPS Spreadsheets installation, add the VBA-compatible procedure if needed, and run prt for the active worksheet.
Set a dynamic print area for A1 through the last populated row in column D.Use landscape orientation and one-page-wide print scaling for wide tables.Open and edit Microsoft Excel XLSX and macro-enabled XLSM workbook formats.Use a familiar spreadsheet interface for printing, page setup, and workbook migration.
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro use column D to find the last row?

The line ws.Cells(ws.Rows.Count, "D").End(xlUp).Row searches upward from the bottom of column D and returns its last nonblank row. Use another column letter if a different column is more reliably populated.

How can I preview the pages instead of printing immediately?

Replace rngPrint.PrintOut with rngPrint.PrintPreview. You can then inspect the landscape layout and one-page-wide scaling before sending the document to a printer.

Will this macro force the entire worksheet onto one physical page?

No. FitToPagesWide = 1 limits the output to one page across. FitToPagesTall = False allows additional pages vertically when there are many rows.

Why is Zoom set to False in the PageSetup block?

Excel uses FitToPagesWide and FitToPagesTall only when manual zoom is disabled. Setting .Zoom = False ensures the one-page-wide setting is applied.

Can I use this macro in a cloud-stored workbook?

Yes, if you open the workbook in the desktop version of Excel and enable macros. Browser-based Excel does not run VBA macros.