Change pivot field with a variable in VBA

cogswel__cogs

Board Regular
Joined
Jan 3, 2018
Messages
177
Hi,
I want to se a variable to switch value of pivot field. I recorded a macro and was going to incorporate it into my own code.
But I think I need to do add quotes. How do I add the quotes so the macro will not fail at line 4?


Sub Macro2()
Dim b As String

b = 1003173578

With ActiveSheet.PivotTables("PivotTable5").PivotFields("Billing Provider NPI")
.PivotItems(b).Visible = True
End With
' With ActiveSheet.PivotTables("PivotTable5").PivotFields("Billing Provider NPI")
' .PivotItems("1003173758").Visible = False
' End With
Range("I26").Select
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If you are setting a string variable, you put the quotes around the value.

b = "1003173578"
 
Upvote 0
that doesn't seem to work
Did the code you commented out work?

Also, I just noticed your strings are not the same:

b = "1003173578"

With ActiveSheet.PivotTables("PivotTable5").PivotFields("Billing Provider NPI")
.PivotItems(b).Visible = True
End With
' With ActiveSheet.PivotTables("PivotTable5").PivotFields("Billing Provider NPI")
' .PivotItems("1003173758").Visible = False
 
Upvote 0

Forum statistics

Threads
1,221,537
Messages
6,160,406
Members
451,644
Latest member
hglymph

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