logo
search
VBA & Macro Problems

How to Export Only Filtered Excel Rows to PDF with VBA

Maira MehtabMaira Mehtab Sep 21, 2026 869 views

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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to launch the Visual Basic for Applications (VBA) editor.

2
Locate the Data Transfer Loop

Find the specific For or Do loop in your macro that copies cell values from the source sheet to the destination range.

3
Add the Visibility Check

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`.

4
Copy Cells and Increment Destino

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`).

5
Close the Statement and Export

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.

Understanding Variable Names: In this context, 'Fila' represents your source row variable and 'FilaDestino' is your target row variable. Replace these with the actual variable names used in your script.
Efficient Data Automation

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. 1. Download WPS Office: Install the latest version of WPS Office and ensure the VBA module is enabled.
  2. 2. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheet and open the .xlsm file containing your filtered data and macro script.
  3. 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.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) formats and macros.Native VBA support to execute and debug custom automation scripts.Built-in PDF conversion features to easily export visible spreadsheet areas.Lightweight interface that handles large, filtered datasets smoothly.
microsoft office alternative - wps office

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.