excel macro vba Filtering using wildcards

3k Views Asked by At

I need to filter in pivot. I need to deselect all items starting with "A." and "H." and keep everything else selected.

the items ranges from: A.(3-13 characters) B.(3-13 characters) all the way to Z.(3-13 characters)

the raw data also changes from 50-500 rows (I can have a data with only 50 rows today - then tomorrow I may have over 500)

my current code works: (by entering all possible items which appears on that column - roughly over 300 items) its long but it works. lately I've been getting more items adding to that list, and I know we can use wild cards.

[MY CURRENT CODE]
    ActiveSheet.PivotTables("PivotTable1").PivotFields("column").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("column"). _
        EnableMultiplePageItems = False
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("column")
    .PivotItems(" PLACE ITEM HERE ").Visible = False
    .PivotItems
    .PivotItems
    .PivotItems [repeat over 300 times - changing the " PLACE ITEM HERE " with the items on the column]
    End With

I can cut down the 300+ lines to about 5-10 lines only, I was thinking about something like (below) it would also fix my problem of having new items that are not yet on my list:

ActiveSheet.PivotTables("PivotTable1").PivotFields("column").ClearAllFilters
        ActiveSheet.PivotTables("PivotTable1").PivotFields("column"). _
        EnableMultiplePageItems = False
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("column")
    .PivotItems("A.*").Visible = False
    .PivotItems("H.*").Visible = False
    End With

but this is not working

1

There are 1 best solutions below

4
On BEST ANSWER

Try this:

With ActiveCell.PivotTable.PivotFields("Column")
        For i = 1 To .PivotItems.Count
            If .PivotItems(i).Name like "A.*" or .PivotItems(i).Name like "H.*" Then
                .PivotItems(i).Visible = True
            else
                .PivotItems(i).Visible = False
            End If
        Next i
    End With