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.
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)
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
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 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.