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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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