Excel Pivot table error 1004 when create the pivot - excel

I am trying to create a pivot table, I found some code and I try to change it for my need, I take the error “1004” Application-defined or Object-defined error when I run the code, can anyone help?
Sub SalesNew()
Application.ScreenUpdating = False
Dim BACKORDER, Analysis As Workbook
Set BACKORDER = Workbooks("BACKORDER2019.xlsx")
Set Analysis = Workbooks("Analysis.xlsx")
Dim wsBACK As Worksheet
Set wsBACK = BACKORDER.Worksheets("Sheet1")
Dim RngBACKstart, RngBACKend, RngBACKnew As Range
Set RngBACKstart = wsBACK.Range("G4")
Set RngBACKend = wsBACK.Cells(wsBACK.Rows.Count, "G").End(xlUp)
Set RngBACKnew = Range(RngBACKstart, RngBACKend)
With RngBACKnew
.NumberFormat = "General"
.Value = .Value
End With
With BACKORDER
Sheets.Add(Before:=Worksheets(Worksheets.Count)).Name = "Pivot"
End With
Dim PSheet As Worksheet
Set PSheet = BACKORDER.Worksheets("Pivot")
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim FirstRow As Range
Dim LastCol As Range
Set FirstCell = wsBACK.Range("A3")
Set LastCell = wsBACK.Cells(wsBACK.Rows.Count, "H").End(xlUp)
Set PRange = Range(FirstCell, LastCell)
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="SalesPivotTable")
Application.ScreenUpdating = False
End Sub

Related

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

My code is my attempt at opening a workbook, then create a pivot table based on a data range on a tab titled "data".
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTBookY As PivotTable
Dim PRange As Range
Dim lastRow As Long
Dim LastCol As Long
Application.ScreenUpdating = False
Set UKBook = _
Workbooks.Open _
("File Path")
Worksheets("Data").Visible = True
Sheets.Add
ActiveSheet.Name = "B22"
Set PSheet = ActiveWorkbook.Worksheets("B22")
Set DSheet = ActiveWorkbook.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.Address)
'Insert Blank Pivot Table
Set PTBookY = PSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=PSheet.Range("A1"), TableName:="PTBookY")
I have additional code past this to enter rows/columns/values for the pivot table, but don't believe it is relevant.
I inconsistently receive
Run-time error 1004: The PivotTable field name is not valid"
when inserting the blank pivot table.
I noticed the code will go through more consistently if I have the workbook open and am on the Data tab.
The data range has a header in every column, and the file path and sheet names are correct.
You really shouldn't rely on ActiveWorkbook. If you're working with the UKBook, then specify that. Change:
Worksheets("Data").Visible = True
Sheets.Add
ActiveSheet.Name = "B22"
Set PSheet = ActiveWorkbook.Worksheets("B22")
Set DSheet = ActiveWorkbook.Worksheets("Data")
...
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange.Address)
to
UKBook.Worksheets("Data").Visible = True
Set PSheet = UKBook.Sheets.Add()
PSheet.Name = "B22"
Set DSheet = UKBook.Worksheets("Data")
...
'Define Pivot Cache
Set PCache = UKBook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange.Address)

My code isn't creating a pivot table for some reason

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
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")
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)
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="SalesPivotTable")
I don't get any error but it just creates the sheet "PivotTable", but no Pivot Table is created on the sheet. Can anyone help? I want the table to use all used cells on the data sheet.

Issue creating Pivot Table using Excel VBA

I've been wanting to create a pivot table off of a sheet called "Data". I worked on the code at first and everything worked out, but for some reason I now get the following error:
Run-time error '440' Method 'Create" of object 'PivotCaches' failed
Here's my code:
Option Explicit
Dim wb As Workbook
Dim wsData As Worksheet, wsPT As Worksheet
Sub Create_Pivot_Table()
Dim LastRow As Long, LastColumn As Long
Dim DataRange As range
Dim PTCache As PivotCache
Dim PT As PivotTable
Set wb = ThisWorkbook
Set wsData = ThisWorkbook.Worksheets("Data")
Call Delete_PT_Sheet
With wsData
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
Set DataRange = .range(.Cells(1, 1), .Cells(LastRow, LastColumn))
Set wsPT = wb.Worksheets.Add
wsPT.Name = "Pivot Table"
Set PTCache = wb.PivotCaches.Create(xlDatabase, DataRange)
Set PT = PTCache.CreatePivotTable(wsPT.range("B5"), "PIVOT")
With PT
'// Pivot Table Layout Settings
.RowAxisLayout xlTabularRow
.ColumnGrand = False
.RowGrand = False
.HasAutoFormat = False
'//Row Section (Layer 1)
With .PivotFields("Helper")
.Orientation = xlRowField
.Position = 1
.LayoutBlankLine = False
End With
'// Values Section
With .PivotFields("Quantity")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##;(#,##);-"
End With
End With
End With
wsPT.Cells.EntireColumn.AutoFit
'//Releasing object memories
Set PTCache = Nothing
Set wsPT = Nothing
Set DataRange = Nothing
Set wsData = Nothing
Set wb = Nothing
End Sub
Private Sub Delete_PT_Sheet()
On Error Resume Next
Application.DisplayAlerts = False
wb.Worksheets("Pivot Table").Delete
End Sub
Could someone review the code and help me to fix it?
Thanks in Advance!

Trying to get Macro to automatically adjust DataRange for Pivot Table

I'm sorry if this is basic but I can't seem to figure this out .
Long story short my file has several tabs with lot's of data and each tab has a pivot table. What I need is a Macro that adjusts the data range of the Pivot table to the data in current sheet. I have it now set with a bigger range giving me Blanks which drives me nuts.
Here is my code :
Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long
'Set Pivot Table & Source Worksheet
Set Data_Sheet = ThisWorkbook.Worksheets("1")
Set Pivot_Sheet = ThisWorkbook.Worksheets("1")
'Enter in Pivot Table Name
PivotName = "PivotTable1"
'Defining Staring Point & Dynamic Range
Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("V2")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange _
, Version:=6)
Now when I try this I get the "Run time error -2147024809" error stating that The pivottable name is invalid .
This updates all the sheets that have 1 pivot table
Sub Update()
Dim wb As Workbook, ws As Worksheet
Dim rng As Range, cell As Range, newrange As String
Set wb = ActiveWorkbook
For Each ws In wb.Sheets
With ws
If .PivotTables.Count = 1 Then
Set cell = .Range("V2").End(xlToRight).End(xlDown)
Set rng = .Range("V2", cell)
newrange = "'" & ws.Name & "'!" & rng.Address(ReferenceStyle:=xlR1C1)
.PivotTables(1).ChangePivotCache _
wb.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=newrange, Version:=6)
End If
End With
Next
End Sub

Get data pivot table VBA

I have generated a pivot table with the following code.
Sub TCD()
Dim last_row As Integer
last_row = Cells(Rows.Count, 1).End(xlUp).Row
Dim My_range As Range
Set My_range = Range(Cells(1, 1), Cells(last_row, 2))
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim pf_Name As String
'Create a new worksheet
Set sht = Sheets.Add
sht.Name = "TCD"
'Where do you want Pivot Table to start?
StartPvt = "TCD" & "!" & sht.Range("A1").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=My_range)
'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable1")
pvt.PivotFields("Type_c").Orientation = xlRowField
pf_Name = "Max iteration par type"
pvt.AddDataField pvt.PivotFields("Nb_Iteration"), pf_Name, xlMax
'Part of code that does nothing
Dim dbl As Double
dbl = pvt.GetData(Name:="CP")
End Sub
The pivot table looks like this
How can I retrieve the number of each row element ? I have tried using get data (last two lines of code) and getpivotdata. Nothing seems to work.
You can try the following:
dbl = pvt.GetData("'Max iteration par type' CP")

Resources