I created a code that based on a set of data creates a Pivot table in a new sheet. The problem is that sometimes it skips the creation of the pivot table.
That skipping only ocurrs in excell 2013, on excel 2016 it always creates the pivot tables.
I checked regarding size but it creates bigger files 100.000 items per example while the one skiping is 80.000
'Define Data Range
lastrow = DSheet.Cells(rows.Count, 2).End(xlUp).Offset(1, 0).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, _
Version:=xlPivotTableVersion15). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:=NameTable)
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:=NameTable)
It should create a pivot table in the new sheet
Related
This question already has an answer here:
Type mismatch during creation of pivot table : Excel vba
(1 answer)
Closed 2 years ago.
When trying to run this code, I continue to get
run time error 13 "Type Mismatch"
when setting the pivot Cache and I am not sure what is causing the issue. I have tried both PivotCaches.Add and PivotCaches.Create and both give the same error. Any Ideas?
Sub NEWPIVOTERROR()
'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
' Assembly Engineer
Sheets("Assembly Engineer").Activate
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Assembly Engineer Charts"
Set PSheet = Worksheets("Assembly Engineer Charts")
Set DSheet = Worksheets("Assembly Engineer")
'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.Add _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="AssemblyEngineerPivotTable")
'?Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="AssemblyEngineerPivotTable")
'Insert Row Fields
With ActiveSheet.PivotTables("AssemblyEngineerPivotTable").PivotFields("Task Owner2")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("AssemblyEngineerPivotTable").PivotFields("Type")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
ActiveSheet.PivotTables("AssemblyEngineerPivotTable").AddDataField ActiveSheet.PivotTables("AssemblyEngineerPivotTable").PivotFields("Type")
End Sub
Sub NEWPIVOTERROR()
'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
' Assembly Engineer
Sheets("Assembly Engineer").Activate 'Edit
Application.DisplayAlerts = False
Worksheets("Assembly Engineer Charts").Delete 'Edit
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Assembly Engineer Charts" 'Edit
Application.DisplayAlerts = True
Set PSheet = Worksheets("Assembly Engineer Charts") 'Edit
Set DSheet = Worksheets("Assembly Engineer") 'Edit
'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:="AssemblyEngineerPivotTable") 'Edit
'?Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="AssemblyEngineerPivotTable") 'Edit
'Insert Row Fields
With ActiveSheet.PivotTables("AssemblyEngineerPivotTable").PivotFields("Task Owner2") 'Edit
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("AssemblyEngineerPivotTable").PivotFields("Type") 'Edit
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
ActiveSheet.PivotTables("AssemblyEngineerPivotTable").AddDataField ActiveSheet.PivotTables("AssemblyEngineerPivotTable").PivotFields("Type") 'EditX2
End Sub
Using a Range object doesn't work well with big datasets. Use the address instead:
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:="'" & DSheet.Name & "'!" & PRange.Address(Referencestyle:=xlr1c1)). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="AssemblyEngineerPivotTable")
I am trying to copy a pivot table to an existing sheet.
I have successfully copied a previous pivot table to this sheet, which is causing me issues when copying a second one.
Current error message is:
Run-time error '1004': Application-defined or object-defined error.
I want to paste the pivot table in cell D64, as the cells below and to the right of this cell are all clear. My existing pivot table is in cell A64:B36.
Can anyone help figure out what I am doing wrong?
Sub ObsoPivotCopy
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
Set PSheet = Worksheets("1188 MJ Summary")
Set DSheet = Worksheets("ObsoCopy")
'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(200, 200), _
TableName:="ObsoPivot")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(200, 200), TableName:="ObsoPivot")
'Insert Data Fields
ActiveSheet.PivotTables("ObsoPivot").AddDataField ActiveSheet.PivotTables( _
"ObsoPivot").PivotFields("MARGIN €"), "Sum of MARGIN €", xlSum
ActiveWindow.SmallScroll Down:=21
ActiveSheet.PivotTables("ObsoPivot").PivotFields("Sum of MARGIN €"). _
Orientation = xlHidden
ActiveSheet.PivotTables("ObsoPivot").AddDataField ActiveSheet.PivotTables( _
"ObsoPivot").PivotFields("NET MARGIN €"), "Sum of NET MARGIN €", xlSum
End Sub
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Prange). _
CreatePivotTable(TableDestination:=PSheet.Cells(200, 200), TableName:="ObsoPivot")
you're creating a pivotcache and then directly calling CreatePivotTable on that cache: that will return a PivotTable, not a PivotCache...
So you only need the first part:
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Prange)
I am writing a code that will create a pivot table based on a dynamic range. I am having issues creating the pivot cache, however. When I run the code, no error message appears, but a blank worksheet is created with no pivot table. I think the issue is the pivot cache but could be wrong. Any ideas?
I've stepped through the code several times but cannot find a bug. Everything appears to be working as it should, except no pivot table appears.
Option Explicit
Dim pivotSht As Worksheet
Dim dataSht As Worksheet
Dim pCache As PivotCache
Dim pTable As PivotTable
Dim pRange As Range
Dim lastR As Long
Dim lastC As Long
Public Sub buildPivot()
'CREATES PIVOT TABLE FROM OPEN ORDER BOOK DATA
'Deletes old sheet, if available, and creates a new one
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set pivotSht = Worksheets("PivotTable")
Set dataSht = Worksheets("OOB")
'Defines data range in "OOB" sheet
lastR = dataSht.Cells(Rows.Count, "D").End(xlUp).Row
lastC = dataSht.Cells(4, Columns.Count).End(xlToLeft).Column
Set pRange = dataSht.Range("D1").Resize(lastR, lastC)
'Define pivot cache
Set pCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=pRange). _
CreatePivotTable(tabledestination:=pivotSht.Cells(3, 1), _
TableName:="OpenOrderBookTable")
'Insert blank pivot table
Set pTable = pCache.CreatePivotTable _
(tabledestination:=pivotSht.Cells(3, 1), TableName:="OpenOrderBookTable")
'Insert row fields in pivot table
With ActiveSheet.PivotTables("OpenOrderBookTable").PivotFields("Machine")
.Orientation = xlRowField
.Position = 1
End With
'Insert column fields in pivot table
With ActiveSheet.PivotTables("OpenOrderBookTable").PivotFields("OTD")
.Orientation = xlColumnField
.Position = 1
End With
'Insert data fields
ActiveSheet.PivotTables("OpenOrderBookTable").AddDataField ActiveSheet.PivotTables( _
"OpenOrderBookTable").PivotFields("OTD"), "Count of OTD", xlCount
With ActiveSheet.PivotTables("OpenOrderBookTable").PivotFields("System Status")
.Orientation = xlPageField
.Position = 1
End With
I expect a pivot table with my range on a new worksheet but am not getting anything.
Try the modified code below:
Public Sub buildPivot()
'CREATES PIVOT TABLE FROM OPEN ORDER BOOK DATA
'Deletes old sheet, if available, and creates new one
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set pivotSht = Worksheets("PivotTable")
Set dataSht = Worksheets("OOB")
'Defines data range in "OOB" sheet
With dataSht
lastR = .Cells(.Rows.Count, "D").End(xlUp).Row
lastC = .Cells(4, .Columns.Count).End(xlToLeft).Column
Set pRange = .Range(.Cells(1, "D"), .Cells(lastR, lastC))
End With
'Define pivot cache
Set pCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pRange.Address(False, False, xlA1, xlExternal))
'Insert blank pivot table
Set pTable = pivotSht.PivotTables.Add(PivotCache:=pCache, TableDestination:=pivotSht.Range("A3"), TableName:="OpenOrderBookTable")
'Insert row fields in pivot table
With pTable.PivotFields("Machine")
.Orientation = xlRowField
.Position = 1
End With
'Insert column fields in pivot table
With pTable.PivotFields("OTD")
.Orientation = xlColumnField
.Position = 1
End With
'Insert data fields
pTable.AddDataField pTable.PivotFields("OTD"), "Count of OTD", xlCount
With pTable.PivotFields("System Status")
.Orientation = xlPageField
.Position = 1
End With
End Sub
My code for doing a Pivot Table doesn't actually do anything. It just creates a new sheet, but not the pivot table.
In my workbook I will be having 2 sheets: one with the data and one with the pivot table.
This is my code:
Sub InsertPivotTable()
'Macro By ExcelChamps
'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
'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")
'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:="Pivot_table")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Pivot_table")
With ActiveSheet.PivotTables("Pivot_table").PivotFields("data1")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("Pivot_table").AddDataField ActiveSheet.PivotTables( _
"Pivot_table").PivotFields("data2"), "Count of data2", xlCount
'Format Pivot Table
ActiveSheet.PivotTables("Pivot_table").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("Pivot_table").TableStyle2 = "PivotStyleMedium9"
End Sub
I am creating a macro which automates creating a pivot table. I am able to get an output for the pivot table, but all the data values are #N/A. This is due to the column that I utilize in the pivot table that contain values of #N/A.
How do I get the actual summation for all rows, without having to temper with the data set? This is the code that I am using so far.
Sub pivottable()
Dim PSheet As Worksheet, DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As pivottable
Dim PField As PivotField
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
Dim PvtTable As pivottable
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(1)
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)
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(4, 1), _
TableName:="PivotTable4")
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")
Sheets("PivotTable").Select
Set PvtTable = ActiveSheet.PivotTables("PivotTable")
'Rows
With PvtTable.PivotFields("Office")
.Orientation = xlRowField
.Position = 1
End With
With PvtTable.PivotFields("Type")
.Orientation = xlRowField
.Position = 2
End With
'Columns
With PvtTable.PivotFields("Category")
.Orientation = xlColumnField
.Position = 1
End With
PvtTable.AddDataField PvtTable.PivotFields("Receipts"), "receipts", xlSum
End Sub
I read that it might be possible to use AutoFilter . Would AutoFilter be able to ignore data with #N/A values or "0" and carry on summing other values, so we avoid having #N/A in our pivot table.
Wrap your VLOOKUP function in an IFERROR function, so that #N/A values are not returned in your source data.