Jagdev Singh
Active Member
Hi Experts
I am facing 1004 error when trying to run the following pivot code.
The code on which I am getting an error is below:
"ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R" & LastRow & "C" & LastCol, Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Pivot!R6C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14"
I have attached the error screen-shot with the post.
Sub createPivotTable1()
'refer Image DataSource for source data. Note that the complete data table extends upto row number 49.
'refer Image 1 for PivotTable report created after running below code
Dim PvtTbl As PivotTable
Dim wsData As Worksheet
Dim pivotrng As Range
Dim lrow As Long, lCol As Long
Dim wsPvtTbl As Worksheet
Application.ScreenUpdating = False
'Worksheet which contains the source data
Set wsData = Worksheets("Data")
'Worksheet where the new PivotTable will be created
Set wsPvtTbl = Worksheets("Pivot")
'delete all existing Pivot Tables in the worksheet
'in the TableRange1 property, page fields are excluded; to select the entire PivotTable report, including the page fields, use the TableRange2 property.
For Each PvtTbl In wsPvtTbl.PivotTables
If MsgBox("Delete existing PivotTable!", vbYesNo) = vbYes Then
PvtTbl.TableRange2.Clear
End If
Next PvtTbl
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R" & LastRow & "C" & LastCol, Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Pivot!R6C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1")
'Default value of ManualUpdate property is False wherein a PivotTable report is recalculated automatically on each change. Turn off automatic updation of Pivot Table during the process of its creation to speed up code.
PvtTbl.ManualUpdate = True
Set pvtFld = PvtTbl.PivotFields("Assigned")
pvtFld.Orientation = xlRowField
pvtFld.Position = 1
'Set pvtFld = PvtTbl.PivotFields("Assigned")
'pvtFld.Orientation = xlColumnField
'set data field - specifically change orientation to a data field and set its function property:
With PvtTbl.PivotFields("Broker Ref 1 (Policy Ref)")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "0"
.Position = 1
End With
With PvtTbl.PivotFields("Assigned")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "0"
.Position = 1
End With
PvtTbl.PivotFields("Broker Ref 1 (Policy Ref)").AutoSort Order:=xlAscending, Field:="Count of name"
'turn on automatic update / calculation in the Pivot Table
PvtTbl.ManualUpdate = False
End Sub
I am facing 1004 error when trying to run the following pivot code.
The code on which I am getting an error is below:
"ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R" & LastRow & "C" & LastCol, Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Pivot!R6C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14"
I have attached the error screen-shot with the post.
Sub createPivotTable1()
'refer Image DataSource for source data. Note that the complete data table extends upto row number 49.
'refer Image 1 for PivotTable report created after running below code
Dim PvtTbl As PivotTable
Dim wsData As Worksheet
Dim pivotrng As Range
Dim lrow As Long, lCol As Long
Dim wsPvtTbl As Worksheet
Application.ScreenUpdating = False
'Worksheet which contains the source data
Set wsData = Worksheets("Data")
'Worksheet where the new PivotTable will be created
Set wsPvtTbl = Worksheets("Pivot")
'delete all existing Pivot Tables in the worksheet
'in the TableRange1 property, page fields are excluded; to select the entire PivotTable report, including the page fields, use the TableRange2 property.
For Each PvtTbl In wsPvtTbl.PivotTables
If MsgBox("Delete existing PivotTable!", vbYesNo) = vbYes Then
PvtTbl.TableRange2.Clear
End If
Next PvtTbl
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R" & LastRow & "C" & LastCol, Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Pivot!R6C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1")
'Default value of ManualUpdate property is False wherein a PivotTable report is recalculated automatically on each change. Turn off automatic updation of Pivot Table during the process of its creation to speed up code.
PvtTbl.ManualUpdate = True
Set pvtFld = PvtTbl.PivotFields("Assigned")
pvtFld.Orientation = xlRowField
pvtFld.Position = 1
'Set pvtFld = PvtTbl.PivotFields("Assigned")
'pvtFld.Orientation = xlColumnField
'set data field - specifically change orientation to a data field and set its function property:
With PvtTbl.PivotFields("Broker Ref 1 (Policy Ref)")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "0"
.Position = 1
End With
With PvtTbl.PivotFields("Assigned")
.Orientation = xlDataField
.Function = xlCount
.NumberFormat = "0"
.Position = 1
End With
PvtTbl.PivotFields("Broker Ref 1 (Policy Ref)").AutoSort Order:=xlAscending, Field:="Count of name"
'turn on automatic update / calculation in the Pivot Table
PvtTbl.ManualUpdate = False
End Sub