OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for anyone's suggestions. I will give feedback on any suggested solutions.
Why am I getting a "Compile error: Invalid Qualifier" on "xlDataField" within the line:
When I comment out that part of the code it runs fine. The following is the entire code.
Why am I getting a "Compile error: Invalid Qualifier" on "xlDataField" within the line:
Code:
'Add item to the Data Field to either Count or Sum. In this Case Sum
With DataPivotTable.PivotFields("Gas.Volume.Gross").Orientation = xlDataField.Function = xlSum
End With
When I comment out that part of the code it runs fine. The following is the entire code.
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
'Checks to see if the new sheet exists "Pivot.Table.Data" and deletes it if so
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Pivot.Table.Data" Then
Sheet.Delete
End If
Next Sheet
'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("B2:" & 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
'Add item to the Report Filter (Reserve Category)
DataPivotTable.PivotFields("RC").Orientation = xlPageField
'Add item to the Column Labels (Date)
DataPivotTable.PivotFields("Date").Orientation = xlColumnField
'Add item to the Row Labels (PHDWin Case Name)
DataPivotTable.PivotFields("Well Name").Orientation = xlRowField
'Add item to the Data Field to either Count or Sum. In this Case Sum
With DataPivotTable.PivotFields("Gas.Volume.Gross").Orientation = xlDataField.Function = xlSum
End With
'Format Pivot Field
'DataPivotTable.PivotFields("Year").NumberFormat = "#,##0"
'Turn on Automatic updates/calculations - like screenupdating to speed up code
DataPivotTable.ManualUpdate = False
End Sub