Printing Problems

How to Print Only Rows with Data to PDF in Excel VBA

Camila MilosovichCamila Milosovich Nov 3, 2026 868 views

Question details

The user needs to modify an Excel VBA macro to export only the rows that contain data into a PDF, avoiding the inclusion of empty rows or blank pages.

How to Print Only Rows with Data to PDF in Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Exporting a dynamic worksheet area to a PDF document using a VBA macro without hardcoding the exact range address.
Observed behavior
A standard macro exporting a fixed range includes empty rows and extra blank pages outside the actual data set.
Before you start

Before running the macro, ensure you clear any formatting (like background colors or borders) from completely empty cells below or beside your data, as Excel may consider them part of the active working area.

Solution 1Recommended

Use the UsedRange Property in VBA

Dynamically set the print area using the worksheet's UsedRange property so that only rows with active data are exported to the PDF.

By utilizing the UsedRange property, your VBA macro can automatically detect the boundaries of your actual data. This prevents Excel from exporting hundreds of blank pages that might exist within a manually specified fixed range.

1
Open the VBA Editor

Open your Excel workbook and press ALT + F11 to launch the Visual Basic for Applications editor.

2
Define the Print Area

Create a new module and define your print area variable by setting it to ActiveSheet.UsedRange.

3
Set Page Setup Properties

Assign the dynamically found range address to the page setup: ActiveSheet.PageSetup.PrintArea = PrintRange.Address.

4
Export to PDF

Use the ExportAsFixedFormat method on the ActiveSheet to save the file. For example: ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Output.pdf".

Watch out for hidden formatting: Even if a cell appears empty, having previous data or hidden formatting can cause Excel to include it in the UsedRange. You may need to manually delete unused rows if blank pages persist.
Smart PDF Export with WPS Spreadsheet

Easily Export Data to PDF Using WPS Office

WPS Spreadsheet provides intuitive built-in tools to export only your active data to PDF without needing complex VBA scripts. It offers high compatibility with Microsoft Excel formats and seamlessly supports VBA macros.

  1. 1. Open your workbook: Launch WPS Spreadsheet and open your Excel workbook.
  2. 2. Highlight the data: Select the specific rows you wish to print, or simply let WPS Spreadsheet automatically detect the used active area.
  3. 3. Access PDF Export: Click on 'Menu' in the top-left corner and select 'Export to PDF'.
  4. 4. Export cleanly: Choose the 'Active sheet' or 'Selection' option and click 'Export' to generate a clean PDF document instantly.
Automatically scales and fits your active data to the PDF pageFully compatible with Microsoft Excel (.xlsx, .xlsm, .xls) filesAdvanced PDF conversion tools built-in for freeRobust support for VBA macros for advanced workflow automation
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA macro printing blank pages at the end of the PDF?

This usually happens when empty cells contain invisible formatting, such as spaces, borders, or text wrapping, causing Excel to include them in the UsedRange. Select the seemingly empty rows below your data, right-click, and choose 'Delete' to clear them entirely.

How can I set the PDF orientation to landscape in VBA?

You can change the page orientation by adding the line `ActiveSheet.PageSetup.Orientation = xlLandscape` in your macro script right before calling the ExportAsFixedFormat method.

Can I use VBA to export only a specific highlighted selection to PDF?

Yes. Instead of using the UsedRange property, you can use `Selection.ExportAsFixedFormat Type:=xlTypePDF` to export only the cells you have manually highlighted with your mouse prior to running the macro.