Set Excel VBA Print Areas and Page Settings for Selected Worksheets
Question details
Set print areas and page settings for worksheets selected through a list box in an Excel workbook.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A workbook button prints selected worksheets whose data extends to different rows in column M.
- Observed behavior
- Each selected worksheet should print from A1 through its last populated row in column M, with landscape orientation, narrow margins, one-page-wide scaling, and its sheet name in the header.
Check that each list box entry matches a worksheet name and that column M identifies the last row to print. Enable macros for the workbook and save it in a macro-enabled format such as .xlsm.
Apply PageSetup to each selected worksheet before printing
Use this VBA approach when a UserForm list box contains the names of worksheets to preview or print.
The example assumes a UserForm named in your existing workbook has a multi-select list box named ListBox1 and a button named CommandButton1. It skips worksheets with no entries in column M.
Excel's &A header code displays the worksheet name. Setting Zoom to False allows FitToPagesWide to control scaling, while FitToPagesTall = False permits multiple pages vertically.
Press Alt+F11 to open the VBA editor. In the Project pane, open your UserForm, select ListBox1, and set its MultiSelect property to allow multiple selections.
In the UserForm code window, add or adapt this button procedure: Private Sub CommandButton1_Click() Dim i As Long Dim ws As Worksheet Dim lastRow As Long For i = 0 To Me.ListBox1.ListCount - 1 If Me.ListBox1.Selected(i) Then Set ws = ThisWorkbook.Worksheets(CStr(Me.ListBox1.List(i))) If Application.WorksheetFunction.CountA(ws.Columns("M")) > 0 Then lastRow = ws.Cells(ws.Rows.Count, "M").End(xlUp).Row With ws.PageSetup .PrintArea = "$A$1:$M$" & lastRow .Orientation = xlLandscape .LeftMargin = Application.InchesToPoints(0.25) .RightMargin = Application.InchesToPoints(0.25) .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False .CenterHeader = "&A" End With ws.PrintPreview End If End If Next i End Sub
Run the UserForm, select worksheet names in ListBox1, and click CommandButton1. Review each Print Preview; replace ws.PrintPreview with ws.PrintOut in the VBA editor to print directly.

Manage spreadsheet print layouts with WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files and adjust print layouts in WPS Spreadsheets for an easier transition; it does not fix Excel VBA or guarantee that Excel macros will run unchanged.

Frequently Asked Questions
How does the macro find the last row to print?
For each selected worksheet, ws.Cells(ws.Rows.Count, "M").End(xlUp).Row finds the last used row in column M. The code then sets PageSetup.PrintArea from A1 through column M on that row.
How do I fit all columns on one printed page without forcing all rows onto one page?
In the worksheet's PageSetup block, set Zoom = False, FitToPagesWide = 1, and FitToPagesTall = False. The sheet can then span multiple pages vertically.
How can I show the worksheet name in the header?
Set .CenterHeader = "&A" inside the worksheet's PageSetup block. Excel replaces &A with the sheet name when displaying or printing the header.
Why does a selected worksheet get skipped?
The example skips a sheet if column M contains no entries. Check column M and the list box entry, which must match the worksheet name in ThisWorkbook.
How do I print instead of previewing each sheet?
In the UserForm's button procedure, replace ws.PrintPreview with ws.PrintOut. Leave the PageSetup block before that line so the settings are applied first.




