How to Export Only Filtered Excel Rows to PDF with VBA
Question details
The user needs a VBA solution to export only the visible, filtered rows from a spreadsheet to a PDF, avoiding the inclusion of hidden rows in the final document.
- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating a PDF report generation from a filtered dataset using a VBA script.
- Observed behavior
- The current VBA macro copies and exports all rows in the dataset, including the hidden ones, instead of strictly transferring only the visible rows.
Ensure you have the Developer tab enabled in your spreadsheet ribbon and identify the exact row variables your current macro uses for the data transfer loop.
Use the Hidden Property in VBA to Copy Only Visible Rows
Modify your VBA macro loop to evaluate the visibility of each row before transferring its contents to the PDF destination range.
When a VBA script iterates through a filtered list, it may process hidden rows unless explicitly instructed otherwise. By checking the `.Hidden` property of each row within your loop, you can ensure that only visible data is copied into the staging area meant for PDF generation.
Press Alt + F11 to launch the Visual Basic for Applications (VBA) editor.
Find the specific For or Do loop in your macro that copies cell values from the source sheet to the destination range.
Wrap your cell copy commands in an IF statement that verifies if the row is visible. Use the syntax: `If Not Rows(Fila).Hidden Then`.
Inside the IF block, copy the required cells (e.g., `Cells(FilaDestino, "K") = Cells(Fila, "C")`) and increment the destination row variable (e.g., `FilaDestino = FilaDestino + 1`).
End the IF block with `End If`, increment your source row variable (`Fila = Fila + 1`), and allow the macro to proceed with exporting the final destination range to PDF.
Run and Edit Macros in WPS Spreadsheet
WPS Office provides robust support for VBA and macros, allowing you to seamlessly filter data, run scripts, and directly export selected ranges to PDF.
- 1. Download WPS Office: Install the latest version of WPS Office and ensure the VBA module is enabled.
- 2. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open the .xlsm file containing your filtered data and macro script.
- 3. Execute the Modified Script: Navigate to the Developer tab, open your Macros list, and run the updated VBA script to generate your PDF from the filtered rows.

Frequently Asked Questions
Why does my Excel VBA macro capture hidden rows?
By default, iterating through a row count or referencing a standard range in VBA will access all cells regardless of their visibility status on the spreadsheet. You must explicitly filter out hidden rows using the .Hidden property or SpecialCells.
Can I use SpecialCells instead of a row-by-row loop?
Yes. Using `Range.SpecialCells(xlCellTypeVisible).Copy` is a highly efficient alternative to loops, as it automatically selects and copies only the visible rows from a filtered dataset in a single command.
How do I export a specific range to PDF using VBA?
You can use the `ExportAsFixedFormat` method directly on a Range object. Set the Type argument to `xlTypePDF` and provide the file path in the Filename argument to save that specific area as a PDF.




