Run-time error '1004': The PivotTable field name is not valid - vba

I am creating an 8th Pivot Table in the same spreadsheet and I am getting the following error for the LAST PIVOT TABLE:
"Run-time error '1004':
The PivotTable field name is not valid. To create a PivotTable report you must use data that is organized as a list of labeled columns. If you are changing the name of a PivotTable field, you must type a new name for the field.
"
However, I have tried to change the name of Pivot Table several times and the issue has not been resolved.
My code is as follows:
Sub Macro1()
'Pivot Table 8
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim LastRow As String
Dim LastCol As String
Dim SData As String
Dim PCache As PivotCache
Dim PTable As PivotTable
Set PSheet = Worksheets("US MASTER")
Set DSheet = Worksheets("US Master Macro")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
SData = "'US Master Macro'!R1C1:R" & LastRow & "C" & LastCol
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=SData)
Set PTable = PCache.CreatePivotTable(TableDestination:=Worksheets("US MASTER").Range("Y4"), TableName:="InfoView Cases")
With ActiveSheet.PivotTables("InfoView Cases")
.SmallGrid = False
'Add Days to Row Field
With .PivotFields("Age of Case")
.Orientation = xlRowField
.Position = 1
End With
'Add PR ID to Values Field
With .PivotFields("PR ID")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
End With
'Add Filter
With .PivotFields("SAP Notification")
.Orientation = xlPageField
.Position = 1
End With
'Add Filter
With .PivotFields("Case Status")
.Orientation = xlPageField
.Position = 2
End With
End With
'Deselect Filter
l = ActiveSheet.PivotTables("InfoView Cases").PivotFields("SAP Notification"). _
PivotItems.Count - 1
For k = 1 To l
With ActiveSheet.PivotTables("InfoView Cases").PivotFields("SAP Notification")
.PivotItems(k).Visible = False
End With
Next k
With ActiveSheet.PivotTables("InfoView Cases").PivotFields("SAP Notification")
.PivotItems("(blank)").Visible = True
End With
'Deselect Filter
n = ActiveSheet.PivotTables("InfoView Cases").PivotFields("Case Status"). _
PivotItems.Count - 1
For m = 1 To n
With ActiveSheet.PivotTables("InfoView Cases").PivotFields("Case Status")
.PivotItems(m).Visible = False
End With
Next m
With ActiveSheet.PivotTables("InfoView Cases").PivotFields("Case Status")
.PivotItems("(blank)").Visible = True
End With
'Add InfoView Cases
PSheet.Range("Y3").Value = "InfoView Cases"
PSheet.Range("Y4").Value = "Days"
'Merge
PSheet.Range("Y3:Z3").Merge
'Sort Pivot Table
Range("Y5:Y100").Select
ActiveSheet.PivotTables("InfoView Cases").PivotFields("Age of Case").AutoSort _
xlAscending, "Age of Case"
End Sub
My code breaks in the line
Set PTable = PCache.CreatePivotTable(TableDestination:=Worksheets("US MASTER").Range("Y4"), TableName:="InfoView Cases")

This answer was provided by Mathieu Guindon:
Dim srcRange As Range
sTable = "DataTable"
Set ListObj = ActiveSheet.ListObjects.Add(xlSrcRange, [A1].CurrentRegion, , xlYes)
ListObj.Name = sTable 'The name for the table
ListObj.TableStyle = "TableStyleDark8"
Dim srcRange_s As String
srcRange_s = ActiveSheet.ListObjects("DataTable")
'Pivot Table 8
'Pivot Table 8
'Pivot Table 8
'Define Pivot Cache and Insert Pivot Table
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=srcRange_s)
Set PTable = PCache.CreatePivotTable(Range("Y4"), TableName:="InfoView Cases")
'Add Fields
With ActiveSheet.PivotTables("InfoView Cases")
.SmallGrid = False
'Add Days to Row Field
With .PivotFields("Age of Case")
.Orientation = xlRowField
.Position = 1
End With
'Add PR ID to Values Field
With .PivotFields("PR ID")
.Orientation = xlDataField
.Function = xlCount
.Position = 1
End With
'Add Filter
With .PivotFields("SAP Notification")
.Orientation = xlPageField
.Position = 1
End With

Related

VBA Code to Create Pivot Table Was Working, Now it is Not

Any help would be appreciated. The Code below was working previously, now it is not. I have the exact same code for a different PIVOT table down below this one and it works perfectly. I'm scratching my head on this one.
'Pivot Tables
Sub TestPivot()
Sheets("Reported Sales").Select
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Declare Variables
Application.DisplayAlerts = False
Worksheets("Pivot Reported Sales").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Pivot Reported Sales"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Pivot Reported Sales")
Set DSheet = Worksheets("Reported Sales")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="Pivot Reported Sales")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(2, 2), TableName:="Pivot Reported Sales")
'Insert Row Fields
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("PROD_BASE_CD")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("Product Description")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("Compensation Category")
.Orientation = xlRowField
.Position = 3
End With
'Insert Data Field
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("PROD_SALES_AMT")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "$#,##0"
End With
With ActiveSheet.PivotTables("Pivot Reported Sales").PivotFields("INVC_DATE")
.Orientation = xlPageField
.Position = 1
End With
End Sub

Is there a way to put multiple pivot tables in the same sheet from the same pivot cache?

I'm trying to put two pivot tables into the same sheet but my second pivot table is not being created when I step into VBA. The code runs without any errors, but the second pivot table is never created. I'm looking to eventually add many pivot tables to the same sheet so any help with this would be appreciated.
I've tried setting two different caches, but I don't believe that will help solve this specific problem. I've also tried
Sub CreatePivottablewithVBA()
'Declare Variables
Dim Psheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim Ptable As PivotTable
Dim Ptable2 As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Insert a New Blank Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set Psheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Raw")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=Psheet.Cells(2, 2), _
TableName:="SalesbyYearMonth")
'Insert Blank Pivot Table
Set Ptable = PCache.CreatePivotTable _
(TableDestination:=Psheet.Cells(1, 1), TableName:="SalesbyYearMonth")
'Insert Row Fields
With ActiveSheet.PivotTables("SalesbyYearMonth").PivotFields("ck_complete_dt")
.Orientation = xlRowField
.Position = 2
End With
'Insert Data Field
With ActiveSheet.PivotTables("SalesbyYearMonth").PivotFields("total_amount")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.Name = "# of Sales"
End With
With ActiveSheet.PivotTables("SalesbyYearMonth").PivotFields("total_amount")
.Orientation = xlDataField
.Position = 2
.NumberFormat = "$#,##0.00"
.Function = xlSum
.Name = "Total Amount"
End With
With ActiveSheet.PivotTables("SalesbyYearMonth").PivotFields("total_amount")
.Orientation = xlDataField
.Position = 3
.Calculation = xlPercentOfTotal
.NumberFormat = "0.00%"
.Name = "% of Total"
End With
'Format Pivot Table
ActiveSheet.PivotTables("SalesbyYearMonth").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("SalesbyYearMonth").TableStyle2 = "PivotStyleMedium9"
PivotTable.DataOnRows = False
'Create PivotTable2
Set Ptable2 = PCache.CreatePivotTable(TableDestination:=Psheet.Cells(22, 2), TableName:="SalesPerCategory")
With ActiveSheet.PivotTables("SalesPerCategory").PivotFields("highest_categ_level")
.Orientation = xlRowField
.Position = 1
.Name = "Category"
End With
With ActiveSheet.PivotTables("SalesPerCategory").PivotFields("highest_categ_level")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.Name = "Count of Categories"
End With
End Sub
I don't receive any error messages but I expect to see both pivot tables created in the sheet PivotTable

Getting #n/a when changing value field settings in Pivot table using vba

I have a use case where I filter the column values in a pivot and then change the value field settings of the pivot table. I am creating the pivot table and the sheet every time while deleting the previous one. My problem is that I am unable to get one column to reflect in the pivot but the values are correct in the grand total. My code is below:
Sub createPivotandFilter()
'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim pf As PivotField
Dim PItems As PivotItem
'Insert a New Blank Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Data_TB Detail")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(3, 1), _
TableName:="VariancePivot")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="VariancePivot")
With ActiveSheet.PivotTables("VariancePivot")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
'Insert Column Fields
With ActiveSheet.PivotTables("VariancePivot").PivotFields("Period")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("VariancePivot").PivotFields("LE")
.Orientation = xlColumnField
.Position = 2
End With
'Insert Row Fields
With ActiveSheet.PivotTables("VariancePivot").PivotFields("Mapped up 1st level")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("VariancePivot").PivotFields("Account")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("VariancePivot").PivotFields("Description")
.Orientation = xlRowField
.Position = 3
End With
'Insert Data Field
With ActiveSheet.PivotTables("VariancePivot").PivotFields("USD")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Sum of USD"
.Calculation = xlDifferenceFrom
.BaseField = "Period"
.BaseItem = "Jun 30,2018"
End With
'adding the filter
Set PTable = ActiveSheet.PivotTables("VariancePivot")
Set pf = PTable.PivotFields("Period")
PTable.RefreshTable
temp = pf.PivotItems.Count
For i = 1 To temp
If pf.PivotItems(i).Value = "Jun 30,2018" Or pf.PivotItems(i).Value = "Sep 30,2018" Then
pf.PivotItems(i).Visible = True
Else
pf.PivotItems(i).Visible = False
End If
Next
End Sub
A screenshot of the result:
Any help is much appreciated.

Run-time error '13': Type mismatch in Macro

I have recorded a macro to create a Pivot Chart. The Data Source is defined as a variable because the data keeps on changing. The macro ran well for a fewer number of rows (up till 20 no.). But as the data increases beyond the 20 no. of rows, the macro shows a
Run-time error 13: Type mismatch.
The code is as below. The highlighted part shows error
Dim s As Worksheet, t As String
Dim i As Long, K As Long
K = Sheets.Count
For i = K To 1 Step -1
t = Sheets(i).Name
If t = "COMPARISON" Then
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
End If
Next i
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "COMPARISON"
Sheets("FRONT PAGE").Select
Nobid = Cells(11, 4)
Sheets("ARRANGED BIDS").Select
Finalrow = Cells(Rows.Count, 1).End(xlUp).Row
**ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("ARRANGED BIDS").Range(Cells(2, 1), Cells(Finalrow, (7 + 2 * Nobid))), Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="COMPARISON!R1C1", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion15**
Sheets("COMPARISON").Select
Cells(1, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("COMPARISON!$A$1:$C$18")
ActiveSheet.Shapes("Chart 1").IncrementLeft 192
ActiveSheet.Shapes("Chart 1").IncrementTop 15
With ActiveChart.PivotLayout.PivotTable.PivotFields("Item No.")
.Orientation = xlRowField
.Position = 1
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("Item Description")
.Orientation = xlRowField
.Position = 2
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("Quantity")
.Orientation = xlRowField
.Position = 3
End With
ActiveWindow.SmallScroll Down:=-6
With ActiveChart.PivotLayout.PivotTable.PivotFields("Item No.")
.Orientation = xlRowField
.Position = 3
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("Quantity")
.Orientation = xlRowField
.Position = 1
End With
Please, someone, help me resolve the error.
Thanks
Try the code below, it should take of you error, and all the PivotTable setting. Explanation are inside the code comments.
Note: I’ve removed all of the unqualified Range and Cells, and also the un-necessary Select, which slows the code down.
Code
Option Explicit
Sub copy_to_report()
Dim PvtTbl As PivotTable
Dim PvtCache As PivotCache
Dim PvtRng As Range
Dim ws As Worksheet
'Dim t As String
Dim i As Long
Dim FinalRow As Long
Dim Nobid As Long
Application.DisplayAlerts = False
' loop through all sheets, if one of them is name "COMPARISON" delete it
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "COMPARISON" Then ws.Delete
Next ws
Application.DisplayAlerts = True
Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
ws.Name = "COMPARISON"
' ***** THIS is the only variable I'm not sure what it's used for *****
Nobid = Sheets("FRONT PAGE").Cells(11, 4)
With Sheets("ARRANGED BIDS")
' find last row in column "A" in "ARRANGED BIDS" sheet
FinalRow = .Cells(.Rows.Count, 1).End(xlUp).Row
' set the Range for the Pivot Cache
Set PvtRng = .Range(.Cells(2, 1), .Cells(Finalrow, (7 + 2 * Nobid)))
End With
' Option 1: Set Pivot Cache
Set PvtCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, PvtRng)
' Option 2: Set Pivot Cache
'Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PvtRng.Address(True, True, xlA1, True))
' create a new Pivot Table in "COMPARISON" sheet
Set PvtTbl = ws.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=ws.Range("A1"), TableName:="PivotTable1")
End Sub
Either you try this...
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("ARRANGED BIDS").Range(Cells(2, 1), Cells(Finalrow, (7 + 2 * Nobid))), Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="COMPARISON!A1", TableName:="PivotTable1"
Or try it like this...
Dim pc As PivotCache
Dim pt As PivotTable
Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("ARRANGED BIDS").Range(Cells(2, 1), Cells(Finalrow, (7 + 2 * Nobid))), Version:=xlPivotTableVersion15)
Set pt = pc.CreatePivotTable(TableDestination:="COMPARISON!A1", TableName:="PivotTable1")
if you want to create a pivot, here is what i use. Remove all the "xl." from code, i was creating them on new excel and saved them.
Dim wb As Workbook
Dim ws As Worksheet
Dim rng1 As Range
Set wb = xl.ActiveWorkbook
Set ws = wb.Sheets("Data")
Set rng1 = ws.Cells.Find("*", ws.[a1], xlFormulas, , , xlPrevious)
xl.Worksheets.Add
xl.ActiveSheet.Name = "Pivot"
xl.Worksheets("Pivot").Activate
xl.ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R" & CStr(rng1.Row) & "C" & CStr(rng1.Column), Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Pivot!R3C1", TableName:="PivotTable3", DefaultVersion _
:=xlPivotTableVersion15
xl.Sheets("Pivot").Select
xl.ActiveWorkbook.ShowPivotTableFieldList = True
With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status")
.Orientation = xlRowField
.Position = 1
End With
With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("CompCode")
.Orientation = xlRowField
.Position = 2
End With
With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("Aging from last action in PD")
.Orientation = xlColumnField
.Position = 1
End With
With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("PD#")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "0"
.Name = "Count of PD#"
End With
xl.ActiveSheet.PivotTables("PivotTable3").HasAutoFormat = False
Dim j As Variant
xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status"). _
EnableMultiplePageItems = True
With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status")
For j = 1 To .PivotItems.Count
DoEvents
.PivotItems(.PivotItems(j).Name).Visible = False
With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status")
On Error Resume Next
.PivotItems("In progress AP Timisoara").Visible = True
End With
Next j
End With

VBA Pivot Table

I am trying to write some VBA that will build a pivot table in a new tab. When I run the code I am getting the Type Mismatch error when creating the PivotTable Cache.
The code is below, any help would be appreciated...hopefully a fresh look at it can spot what I am missing
Sub makeAPivotTable()
Dim sSheet, sSheet2 As Worksheet 'sSheet is where the data is, sSheet2 is where the pivot will be built
Dim cCache As PivotCache
Dim ptTable As PivotTable
Dim rRange As Range
Dim rLastRow, cLastColumn As Long
' Insert the new sheet for the pivot table to reside
Application.DisplayAlerts = False
Sheets.Add Before:=ActiveSheet
ActiveSheet.name = "PivotTable"
Application.DisplayAlerts = True
Set sSheet2 = Worksheets("PivotTable")
Set sSheet = Worksheets("Interactions db")
' define the range (the data that you want to put into the pivot table
rLastRow = sSheet.Cells(Rows.Count, 1).End(xlUp).Row
cLastColumn = sSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set rRange = sSheet.Cells(1, 1).Resize(rLastRow, cLastColumn)
' create the cache for the pivot table
Set cCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=rRange). _
CreatePivotTable(TableDestination:=sSheet2.Cells(2, 2), _
TableName:="SalesPivotTable")
' insert the blank table
Set ptTable = cCache.CreatePivotTable _
(TableDestination:=sSheet2.Cells(1, 1), TableName:="SalesPivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("customer")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
'With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Interaction Type")
'.Orientation = xlColumnField
'.Position = 1
'End With
'Insert Data Field
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("interactionType")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.name = "Person "
End With
' do some formatting
End Sub
Try the code below (explanations inside the code as comments)
Option Explicit
Sub makeAPivotTable()
Dim sSheet As Worksheet, sSheet2 As Worksheet ' sSheet is where the data is, sSheet2 is where the pivot will be built
Dim cCache As PivotCache
Dim ptTable As PivotTable
Dim rRange As Range
Dim rLastRow As Long, cLastColumn As Long ' need to define each one as Long, otherwise the first one is Variant
' Insert the new sheet for the pivot table to reside
Application.DisplayAlerts = False
' 2 lines below are shorter version to create a new sheet,
' assign it to sSheet2 , and name it "PivotTable"
On Error Resume Next
Set sSheet2 = Sheets("PivotTable") '<-- try to set the Sheet (already created in past code runs)
On Error GoTo 0
If sSheet2 Is Nothing Then '<-- sheet does not exist yet >> Add it
Set sSheet2 = Sheets.Add(Before:=ActiveSheet)
sSheet2.Name = "PivotTable"
End If
Application.DisplayAlerts = True
Set sSheet = Worksheets("Interactions db")
' define the range (the data that you want to put into the pivot table
With sSheet
rLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
cLastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rRange = .Cells(1, 1).Resize(rLastRow, cLastColumn)
End With
' create the cache for the pivot table ***** THIS is the CORRECT Syntax
Set cCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rRange)
' CREATE a NEW Pivot Table in sSheet2 sheet
Set ptTable = sSheet2.PivotTables.Add(PivotCache:=cCache, TableDestination:=sSheet2.Range("A1"), TableName:="SalesPivotTable")
With ptTable
'Insert Row Fields
With .PivotFields("customer")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
'With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Interaction Type")
'.Orientation = xlColumnField
'.Position = 1
'End With
'Insert Data Field
With .PivotFields("interactionType")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Person "
End With
' do some formatting
End With
End Sub