How to Filter a PivotTable by Multiple First-Letter Values with VBA
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.

- 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 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.
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.
Press Alt + F11 to open the VBA Editor and locate the module containing your PivotTable macro.
Declare a variable to store the first letter, for example: Dim firstLetter As String.
Inside your PivotItems loop, extract the first letter using the Left function: firstLetter = Left(Pi.Value, 1).
Use a Select Case firstLetter statement to check for your desired letters (e.g., Case "U", "A").
Under the Case Else condition, hide the unmatched items by setting Pi.Visible = False.

Create a Helper Column for Easy Filtering
If you prefer a non-coding approach, add a helper column to your source data to extract the first letter, making it easy to filter via the standard PivotTable interface.
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. Open your file: Launch WPS Spreadsheet and open your workbook containing the PivotTable.
- 2. Manage PivotTables: Navigate to the 'Data' tab and select 'PivotTable' to modify your fields and add helper columns.
- 3. Run your macros: Go to the 'Developer' tab to access the VBA Editor and execute your custom filtering macro.

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.




