How to Filter a Table and Hide Blank Columns using VBA
Question details
The user needs a VBA macro to filter an assembly table based on a search value and dynamically hide any columns that contain blank values in the filtered results.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating table filtering with a search parameter and hiding empty columns to improve data visibility.
- Observed behavior
- The macro must unhide previously hidden columns, apply the AutoFilter, identify the visible filtered row, and hide columns with blanks using Range.SpecialCells without hard-coding rows.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that the Developer tab is active so you can access the VBA Editor.
Use AutoFilter and SpecialCells in VBA
This method clears existing hidden columns, applies a search filter, and dynamically targets blank cells in the visible row to hide their corresponding columns.
Avoid selecting hard-coded row numbers, as the matching row index will change depending on the search criteria. By leveraging the Range.SpecialCells(xlCellTypeBlanks) method on the visible filtered data, the macro can dynamically and safely identify empty cells.
Start your macro by clearing any previously hidden columns using a command like `ActiveSheet.Cells.EntireColumn.Hidden = False` to ensure a clean slate.
Use the `AutoFilter` method on your table range, setting the search criteria based on your designated search cell (e.g., Range("B2").Value).
Set a Range variable to target the visible data row that remains after the filter is applied, explicitly excluding the header row.
Use `On Error Resume Next` before defining your blanks range with `SpecialCells(xlCellTypeBlanks)`. Then, apply `EntireColumn.Hidden = True` to that range and reset error handling with `On Error GoTo 0`.

Automate Table Filtering and Data Cleanup with WPS Spreadsheet
WPS Spreadsheet fully supports standard VBA macros, enabling you to automate repetitive tasks like filtering tables and hiding empty columns seamlessly. Enjoy robust data handling without any extra configuration.
- 1. Enable the Developer Tab: Open WPS Spreadsheet, go to the options, and ensure the Developer tab is enabled to access macro tools.
- 2. Open the VBA Editor: Click on 'Visual Basic' in the Developer tab or press ALT + F11 to launch the integrated VBA editor.
- 3. Insert Your Macro: Right-click on your workbook in the Project window, select 'Insert Module', and paste your AutoFilter and column-hiding VBA code.
- 4. Run and Automate: Save your file as a Macro-Enabled Workbook and run the script directly, or assign it to a worksheet button for one-click filtering.

Frequently Asked Questions
Why does my VBA macro fail when there are no blank cells?
When using `SpecialCells(xlCellTypeBlanks)`, VBA throws a runtime error if no empty cells exist in the specified range. Wrap the command in `On Error Resume Next` and `On Error GoTo 0` to prevent the script from crashing.
How do I unhide all columns before applying a new filter?
You can insert `Cells.EntireColumn.Hidden = False` or `ActiveSheet.UsedRange.Columns.Hidden = False` at the very beginning of your VBA script to reset column visibility before applying the new search parameter.
Can I hide columns based on blanks in multiple filtered rows?
Yes, but it requires iterating through the visible rows or columns individually. If your filter returns multiple rows and you want to hide a column only if it is completely blank across all visible rows, `SpecialCells` applied to a single row will not suffice. You will need a custom loop to evaluate column contents across the visible range.




