VBA - Error in code - Pivot Cache

candacem

New Member
Joined
Sep 8, 2017
Messages
18
This is my pivot cache but I keep getting an error message :( My google cave isn't helping so hoping one of you genius' are able to help.

Thanks in advance!

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sum_Data!R1C1:R5000C19", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="", TableName:="Pivot!R1C1", DefaultVersion:=xlPivotTableVersion14
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
candacem,

Welcome to the Board.

I'm guessing the statement doesn't like the TableName parameter. You might consider adding...

Code:
Dim nm As String
nm = Sheets("Pivot").Range("A1").Value

...then changing the TableName argument to...

Code:
TableName:=nm

Cheers,

tonyyy
 
Upvote 0
I am still getting an error but it really doesn't like that change! I have tried changing the table name a few times and this code just doesn't want to work :( Error is The PivotTable field name is not valid

Here is the full code, I have made some changes which was from Microsoft...still doesn't work.

Sub Create_Pivot()
'
' Create_Pivot Macro
'
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'
Dim myFirstRow As Long
Dim myLastRow As Long
Dim myFirstColumn As Long
Dim myLastColumn As Long
Dim mySourceData As String
Dim myDestinationRange As String
Dim mySourceWorksheet As Worksheet
Dim myDestinationWorksheet As Worksheet
Dim myPivotTable As PivotTable
Dim nm As String



With ThisWorkbook
Set mySourceWorksheet = .Worksheets("Sum_Data")
Set myDestinationWorksheet = .Worksheets("Pivot")
End With
myDestinationRange = myDestinationWorksheet.Range("A1").Address(ReferenceStyle:=xlR1C1)
myFirstRow = 1
myFirstColumn = 1


With mySourceWorksheet.Cells


myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
myLastColumn = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column


mySourceData = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn)).Address(ReferenceStyle:=xlR1C1)


End With


Set myPivotTable = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=mySourceWorksheet.Name & "!" & mySourceData).CreatePivotTable(TableDestination:=myDestinationWorksheet.Name & "!" & myDestinationRange, TableName:="NewPivotTable")


With myPivotTable
With .PivotFields("Class").Orientation = xlRowField
With .PivotFields("Jan-18")
.Orientation = xlDataField
.Position = 1
.Functiom = xlSum
.NumberFormat = "#,##0.00"
End With
End With


With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End With
End Sub
 
Last edited:
Upvote 0
Check your source data - one or more column headers (first row) are probably blank. To create a pivot table, each column must have a header label.
 
Last edited:
Upvote 0
If the error is "PivotTable field name is not valid", then that should be the only reason. Documentation here.

Get the address value in mySourceData variable and then check first row in that address range.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top