VBA-Referencing another workbook in a Pivot Table Field item

jomili

New Member
Joined
Dec 15, 2009
Messages
33
My pivot table has, in the Data field, an item called MOS2. MOS2 has Expenses for the 12 months of the year. The months are formatted as 01 through 12. In another workbook I've got a list of applicable months. I'm trying to get the syntax right to allow the values from my applicable months to be used by my macro to reset the pivot table to those months, by referencing the other workbook. However, my code fails with a Run-Time error 1004 ' "Unable to get the PivotItems Property of the PivotField Class". I'd appreciate any help in getting the syntax right.

Thanks,
John

Code:
With ActiveSheet.PivotTables("PivotTable1").PivotFields("MOS2")
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D2")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D3")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D4")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D5")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D6")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D7")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D8")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D9")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D10")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D11")).Visible = True
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D12")).Visible = True
End With
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
MarvinP on another forum has provided the answer I needed. Amazing how the little word "text" can make such a difference.
Code:
.PivotItems(Workbooks("2BDeleted.xls").Sheets("Lookups").Range("$D2").Text).Visible = True
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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