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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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