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

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.

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)

Invalid Procedure call when creating a pivot table

I was told to try to separate the creation of the cache and table. That worked in getting rid of some of the errors. Now I am getting the invalid procedure call. I've looked to see if other solutions can solve the issue, but I cannot figure it out. I am running office 365 if that matters. Here is my code.
thanks!
Sub Create_101102_pivot_CFG_MV_CL()
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 wb As Workbook
Set wb = Workbooks("TTB - Movements - CFG_CL")
Set DSheet = wb.Worksheets("101-102 F")
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 PSheet = wb.Worksheets("Pivot 101-102 Production")
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
Set PTable = PCache.CreatePivotTable(TableDestination:="'Pivot 101-102 Production'!R2C2", TableName:="101102F_Pivot Table")

VBA to copy Pivot Table Data into another sheet + change headers

I am trying to copy the data in plain format into another sheet, and do further transformation on it.
While I can create the Pivot Table, but whenever I try to copy it into another sheet, the new sheet is empty. Wondering if any experts can point me to where I'm doing this wrongly. Below are my codes for reference:
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim CSheet 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("Pivot_Table").Delete
Worksheets("Cleaned_Data").Delete
Worksheets("RAW_DATA").Activate
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Pivot_Table"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Pivot_Table")
Set DSheet = Worksheets("RAW_DATA")
'*****************************************************
' Define data range for pivot
'*****************************************************
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PivotRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'*****************************************************
' Create pivot cache
'*****************************************************
Set PivotCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange).CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="UserPivotTable")
'*****************************************************
' Insert blank pivot
'*****************************************************
Set PTable = PivotCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="UserPivotTable")
'*****************************************************
' Insert row fields
'*****************************************************
With ActiveSheet.PivotTables("UserPivotTable").PivotFields("userid")
.Orientation = xlRowField
.Position = 1
End With
'*****************************************************
' Insert data field
'*****************************************************
With ActiveSheet.PivotTables("UserPivotTable").PivotFields("QTY")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
'.NumberFormat = "#,##0"
'.Name = "Revenue "
End With
'*****************************************************
' Copy data into another sheet for cleaning
'*****************************************************
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Cleaned_Data"
Application.DisplayAlerts = True
Set CSheet = Worksheets("Cleaned_Data")
PTable.TableRange2.Copy
CSheet.Range("A1").PasteSpecial xlPasteValues
Also with that, how should I customize the headers once I have copied the data into the new sheet?
For consistency, add the missing variables to your dim section:
Dim pivotRange As Range
Dim pivotCache As pivotCache
And change your second last line, the copy line to:
PSheet.PivotTables("UserPivotTable").TableRange2.Copy
If you debug your code, the you'll see, that PivotCache is Nothing.
Your code row Set PivotCache = is just too long.
As you only need the reference to a new PivotCache there, use this:
Set PivotCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotRange)
Further hints:
You should not use VBA-internal names as variable names.
See this concerning further formatting.

Excel Pivot table error 1004 when create the pivot

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

How can I create a dynamic range for a pivot table?

I am trying to write a macro that creates a pivot table on a new sheet based on data from a master spreadsheet. How can I adjust my code to do this?
I've gotten the code to work with static data but whenever the data is dynamic, the macro does not work. I believe the issue is with the first line starting with "srcData = ". The line below, which is commented out, works without any issues.
Option Explicit
Dim pivotSht As Worksheet
Dim dataSht As Worksheet
Dim pCache As PivotCache
Dim pTable As PivotTable
Dim srcData As String
Dim pRange As Range
Dim lastR As Long
Dim lastC As Long
Public Sub buildPivot()
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, "D1"), Cells(lastR, lastC))
End With
Sheets("OOB").Activate
srcData = ActiveSheet.Name & "!" & pRange.Address(ReferenceStyle:=xlR1C1)
'srcData = ActiveSheet.Name & "!" & Range("D1:U714").Address(ReferenceStyle:=xlR1C1)
'Delete old "PivotTable" worksheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
'Create new sheet and name it "PivotTable"
Set pivotSht = Sheets.Add
ActiveSheet.Name = "PivotTable"
'Set location of pivot table
startPvt = pivotSht.Name & "!" & pivotSht.Range("A3").Address(ReferenceStyle:=xlR1C1)
'Define pivot cache
Set pCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=srcData)
'Creates pivot table from pivot cache
Set pTable = pCache.CreatePivotTable( _
tabledestination:=startPvt, _
TableName:="Open Order Book Table")
End Sub
The expected output is a pivot table using all data as the source, even with changing data.

Resources