Change pivot field with a variable in VBA

cogswel__cogs

Board Regular
Joined
Jan 3, 2018
Messages
180
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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
If you are setting a string variable, you put the quotes around the value.

b = "1003173578"
 
Upvote 0
Solution
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
It did work I should have replied and corrected but forgot.
Yes the original reason I was having trouble was due to the error and realized afterward.
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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