How to Print Only Rows with Data to PDF in Excel VBA
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.

- 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 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.
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.
Open your Excel workbook and press ALT + F11 to launch the Visual Basic for Applications editor.
Create a new module and define your print area variable by setting it to ActiveSheet.UsedRange.
Assign the dynamically found range address to the page setup: ActiveSheet.PageSetup.PrintArea = PrintRange.Address.
Use the ExportAsFixedFormat method on the ActiveSheet to save the file. For example: ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="Output.pdf".
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. Open your workbook: Launch WPS Spreadsheet and open your Excel workbook.
- 2. Highlight the data: Select the specific rows you wish to print, or simply let WPS Spreadsheet automatically detect the used active area.
- 3. Access PDF Export: Click on 'Menu' in the top-left corner and select 'Export to PDF'.
- 4. Export cleanly: Choose the 'Active sheet' or 'Selection' option and click 'Export' to generate a clean PDF document instantly.

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.




