Change pivot field with a variable in VBA

cogswel__cogs

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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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