Pivot Table Issues

How to Filter a PivotTable by Multiple First-Letter Values with VBA

Muhammad TalhaMuhammad Talha Oct 28, 2026 868 views

Question details

The user wants to filter a PivotTable based on the first letter of the item names, specifically using more than two different starting letters as criteria within VBA.

How to Filter a PivotTable by Multiple First-Letter Values with VBA
Product
Spreadsheets
Device & OS
not provided
Scenario
Filtering data inside a PivotTable dynamically using custom VBA code to show items starting with specific letters.
Observed behavior
The user needs a method to evaluate and filter PivotItems by multiple starting letter criteria, rather than using full string matches.
Before you start

Before applying VBA code, ensure you have saved a copy of your workbook as a macro-enabled file. It is also recommended to refresh your PivotTable data to avoid processing outdated items.

Solution 1Recommended

Use VBA Select Case to Filter PivotItems by First Letter

Modify your VBA macro to evaluate the first character of each PivotItem and hide the items that do not match your desired letters.

Instead of matching full item strings in your array, you can extract the first letter of each PivotItem and use a Select Case statement to evaluate it against your allowed letters.

1
Open the VBA Editor

Press Alt + F11 to open the VBA Editor and locate the module containing your PivotTable macro.

2
Declare a string variable

Declare a variable to store the first letter, for example: Dim firstLetter As String.

3
Extract the first letter

Inside your PivotItems loop, extract the first letter using the Left function: firstLetter = Left(Pi.Value, 1).

4
Apply Select Case criteria

Use a Select Case firstLetter statement to check for your desired letters (e.g., Case "U", "A").

5
Hide unmatched items

Under the Case Else condition, hide the unmatched items by setting Pi.Visible = False.

Use VBA Select Case to Filter PivotItems by First Letter
Performance Tip: Set Application.ScreenUpdating = False before running the loop to significantly speed up the macro execution when dealing with large PivotTables.
Efficient PivotTable Management

Easily Filter and Manage PivotTables in WPS Spreadsheet

WPS Spreadsheet fully supports advanced PivotTable features, formulas, and VBA macros. You can seamlessly apply helper column formulas or run your filtering scripts to manage large datasets without compatibility issues.

  1. 1. Open your file: Launch WPS Spreadsheet and open your workbook containing the PivotTable.
  2. 2. Manage PivotTables: Navigate to the 'Data' tab and select 'PivotTable' to modify your fields and add helper columns.
  3. 3. Run your macros: Go to the 'Developer' tab to access the VBA Editor and execute your custom filtering macro.
Fully compatible with Microsoft Excel (.xlsx and .xlsm) macro-enabled formats.Supports advanced PivotTable data modeling and custom slicers.Built-in VBA support for automating complex data workflows.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA code returning an error when hiding PivotItems?

This usually happens if you try to hide all PivotItems simultaneously. A PivotTable requires at least one visible item. Ensure your criteria matches at least one item before setting the others to Visible = False.

Can I filter a PivotTable by the last letter instead of the first?

Yes. In VBA, you can use Right(Pi.Value, 1) to extract the last letter. If you are using a helper column, use the formula =RIGHT(cell, 1).

Does WPS Spreadsheet support VBA macros for PivotTables?

Yes, WPS Spreadsheet fully supports VBA macros, allowing you to run scripts for automating PivotTable filters, updates, and formatting exactly as you would in Microsoft Excel.