OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance to anyone who can assist and I will provide feedback on suggested solutions.
For the code to make a Pivot Table, I'm getting the following error:
"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 columns. If you are changing the name of the a PivotTable field, you must type a new name for the field."
On this line of code:
The entire code is as follows:
For the code to make a Pivot Table, I'm getting the following error:
"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 columns. If you are changing the name of the a PivotTable field, you must type a new name for the field."
On this line of code:
Code:
Set DataPivotTable = DataPivotCache.CreatePivotTable(TableDestination:=StartPivot, TableName:="PivotTable1")
The entire code is as follows:
Code:
Sub CreatePivotTable()
'Dimensioning Variables
Dim DataSourceSheet As Worksheet
Dim DataPivotCache As PivotCache
Dim DataPivotTable As PivotTable
Dim StartPivot As String
Dim PivotSourceData As String
Dim ColumnLetter As String
Dim LastRowDF As Long
Dim LastColumnDF As Long
Dim Wks As Worksheet
'Turn off Screen Mirroring/Updating
Application.ScreenUpdating = False
'Ensure all Worksheets within this Workbook are calculated
Application.Calculation = xlManual
'Loop Workbook to calculate all spreadsheets
For Each Wks In ActiveWorkbook.Worksheets
Wks.Calculate
Next
Set Wks = Nothing
'Activate "Date.Formatted" worksheet
Worksheets("Data.Formatted").Activate
'Find last row
LastRowDF = Cells.Find(What:="*", after:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
'Finding last column
LastColumnDF = Cells.Find(What:="*", after:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
'Convert To Column Letter
ColumnLetter = Split(Cells(1, LastColumnDF).Address, "$")(1)
'Determine the data range to pivot
PivotSourceData = Sheets("Data.Formatted").Name & "!" & Range("A1:" & ColumnLetter & LastRowDF).Address(ReferenceStyle:=xlR1C1)
'Creating the new destination sheet "Pivot.Table.Data" as the
ActiveWorkbook.Sheets.Add(after:=Worksheets("Well.Attributes")).Name = "Pivot.Table.Data"
Set DataSourceSheet = Sheets("Pivot.Table.Data")
'Where do you want Pivot Table to start?
StartPivot = DataSourceSheet.Name & "!" & DataSourceSheet.Range("A3").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set DataPivotCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PivotSourceData)
'Create Pivot table from Pivot Cache
Set DataPivotTable = DataPivotCache.CreatePivotTable(TableDestination:=StartPivot, TableName:="PivotTable1")
'Calculate "Pivot.Table.Data"
Sheets("Pivot.Table.Data").Calculate
'Screen Updating On
Application.ScreenUpdating = True
End Sub