I am trying to create a pivot table based on a set range via VBA.
I have defined my 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 PField As PivotField
'Declare Variables
Set PSheet = Worksheets("TS")
Set DSheet = Worksheets("Data Entry")
'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)
PSheet.PivotTables("Timesheet").TableRange2.Clear
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(5, 1), TableName:="Timesheet")
I get an error at insert pivot table code.
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 with labeled colmuns. if you are changing the name of a pivotable field, you must type a new name for the field.
My data range is in DSheet from range A1:P50. Range will not change in the future.
Related
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)
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.
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")
I've added code that I found on here to create a worksheet and populate it with a pivot table. The worksheet gets created every time, but the pivot table does not. No errors messages. Thoughts?
I've tried removing spaces from worksheet names, adding more detailed definitions (from similar codes I found on here)
Private Sub CommandButton2_Click()
'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("MBPivotTable").Delete
Sheets.Add Before:=Worksheets("MB Evaluation")
ActiveSheet.Name = "MBPivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("MBPivotTable")
Set DSheet = Worksheets("MB Raw 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 = Worksheets("MB Raw Data").PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="MBPivotTable")
I would expect a new worksheet named MBPivotTable to be created with a Pivot Table in it. Instead, just the worksheet is created without a pivot table, and no error messages. FYI, I didn't add the entire code here because I have to imagine the error is in this portion, which is why you don't see the "End Sub" portion, but it is there in the full code.
I'm having an error that says: A field in your source data has more unique items that can be used in a PivotTable report.
Basically, if choosing this field it won't create the report. I've just tried it manually since I get the VBA code from that manual step, but the error pops.
My source data has ~45k rows.
This is the code I'm using
Dim pt As PivotTable
Dim wrkSht As Worksheet
Dim pvtSht As Worksheet
Dim PTCache As PivotCache
Dim PRange As Range
Dim finalRow As Long
Dim finalCol As Long
Set wrkSht = Worksheets("Data")
Set pvtSht = Worksheets("Pivot")
finalRow = wrkSht.Cells(Application.Rows.Count, 1).End(xlDown).Row
finalCol = wrkSht.Cells(1, Application.Columns.Count).End(xlToLeft).Column
Set PRange = wrkSht.UsedRange
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)
Set pt1 = PTCache.CreatePivotTable(TableDestination:=pvtSht.Cells(1, 1), TableName:="Pivot")
pt1.RowAxisLayout xlCompactRow
Any help will be appreciated.
Thanks in advance.