Add variable to pivot table code

yeongkwe

New Member
Joined
Dec 13, 2016
Messages
13
I want to add in a variable from pivot table using VBA. Please kindly advise me. Your help will be very much appreciated. Thanks you.
Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+v
'
cdcd = Worksheets("sheet8").Range("AQ2")

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date")
        .PivotItems("cdcd").Visible = True
        .PivotItems("2013").Visible = True
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Remove the quotes round the variable name.
 
Upvote 0
What error do you get with this
Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+v
'
dim cdcd as string
cdcd = Worksheets("sheet8").Range("AQ2")

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date")
        .PivotItems(cdcd).Visible = True
        .PivotItems("2013").Visible = True
    End With
End Sub
 
Upvote 0
Hi, I get this pop outbox stated, Unable to get the pivotiems property of the pivotfield class. But after you send me a good example, I can my dim as cdcd as long to string, the code works already. By the way how come like that? string and long for different in coding? sorry I think I had ask you a stupid question. Thanks for your help. you are really good.
 
Upvote 0
You can refer to PivotItems by name or by index position. If you pass a year as a number, the code will think it is an index position and look for say the 2013th item, rather than the item called "2013". Using a string solves that.
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,541
Members
449,169
Latest member
mm424

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