VBA Pivot table variable

CarolynS

Board Regular
Joined
Oct 27, 2015
Messages
56
Hi

I have the below code to create a pivot table and have set the pivot table as the variable, PT:

Code:
Dim WB As Workbook
Dim WS As Worksheet
Dim WSsb As Worksheet
Dim FinalRow As Long
Dim FinalCol As Integer
Dim PRange As Range
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PF As PivotField
Set WB = ActiveWorkbook
'create pivot table to show status by brand (excluding those marked in franchise id field as exclusions)
    Set WS = Worksheets("Raw_Data")
    Set WSsb = Sheets.Add(after:=Sheets(Sheets.Count))
    WSsb.Name = "Status by Brand"
    
    ' Define input area and set up a Pivot Cache
    FinalRow = WS.Cells(Rows.Count, 1).End(xlUp).Row
    FinalCol = WS.Cells(1, Columns.Count). _
    End(xlToLeft).Column
    Set PRange = WS.Cells(1, 1).Resize(FinalRow, FinalCol)
    Set PTCache = WB.PivotCaches.Add(SourceType:= _
    xlDatabase, SourceData:=PRange)
    ' Create the Pivot Table from the Pivot Cache
    Set PT = PTCache.CreatePivotTable(TableDestination:=WSsb. _
    Cells(1, 1), TableName:="StatusByBrand")
    ' Turn off updating while building the table
    PT.ManualUpdate = True
    ' Set up the row, column & filterfields
    PT.AddFields RowFields:=Array("Brand", "BU"), ColumnFields:="Certification - Status", PageFields:="Person - Franchise ID"
    'set filter
    Set PF = WSsb.PivotTables("StatusByBrand").PivotFields("Person - Franchise ID")
    PF.EnableMultiplePageItems = True
    PF.PivotItems("Yes - 3rd Party").Visible = False
    PF.PivotItems("Yes - Excluded").Visible = False
    ' Set up the data fields
    With PT.PivotFields("Person - Username")
    .Orientation = xlDataField
    .Function = xlCount
    .Position = 1
    End With
    ' Calc the pivot table
    PT.ManualUpdate = False
    'PT.ManualUpdate = True
    
'add columns to status by brand table for calculations
    WSsb.PT.TableRange2.Copy
    WSsb.Range("A1").PasteSpecial (xlPasteValuesAndNumberFormats


PT is working ok to add fields to the table etc but when i try to set the report filter using Set PF = WSsb.PT.PivotFields("Person - Franchise ID") I'm geting an error (method or data member not found) and I've had to change PT to the full pivot table name (PivotTables("StatusByBrand")).

The same is happening at the end of the code for the line WSsb.PT.TableRange2.Copy

Can anyone tell me why this is happening and how i can rectify it without referring to the pivot table by name each time? I am using excel 2007

Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,215,409
Messages
6,124,730
Members
449,185
Latest member
ekrause77

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