pivot table with 'like' operator

bryanrobson

New Member
Joined
Aug 19, 2022
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi. I have the below code which works well. I just need to amend it, so it shows anything with Q in it so *Q*. I tried changing the Is to a Like and also tried changing the "Q" to "*Q*" which it doesn't like.

Many thanks



Sub pivot_test()


Dim pt As PivotTable
Dim pi As PivotItem

Set pt = ActiveSheet.PivotTables("PivotTable1")



For Each pi In _
pt.PivotFields("Mc Type").PivotItems
Select Case pi.Name
Case Is = "Q"
pi.Visible = True
Case Else
pi.Visible = False
End Select

Next pi


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
VBA Code:
pi.visible = (pi.Name Like "*Q*")

should be enough. Note that Like is case-sensitive.
 
Upvote 0
Solution
Hi Rory. So I changed it to below and works like a dream. thanks very much for the quick reply.

Cheers


Sub pivot_test()


Dim pt As PivotTable
Dim pi As PivotItem

Set pt = ActiveSheet.PivotTables("PivotTable1")



For Each pi In _
pt.PivotFields("Mc Type").PivotItems
pi.Visible = (pi.Name Like "*Q*")


Next pi


End Sub
 
Upvote 0
Glad we could help. :)
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,185
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top