I am trying to write code that will create a pivot table from an Excel table, and I keep getting Run-time errors that accuse me of invalid pivot field names.
Sub pivotTracking()
Dim wsTracking As Worksheet
Dim tblTracking As ListObject
Dim pcTracking As PivotCache
Dim ptTracking As PivotTable
Dim ptDestination As Range
Set wsTracking = ActiveWorkbook.Sheets("Tracking")
Set tblTracking = wsTracking.ListObjects("tblTracking")
'Create the cache from the data range
Set pcTracking = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=wsTracking.Range("tblTracking").Address _
)
'Set table destination range
Set ptDestination = wsTracking.Range("P2")
'Create the Pivot table
Set ptTracking = wsTracking.PivotTables.add( _
PivotCache:=pcTracking, _
TableDestination:=ptDestination, _
TableName:="Pivot2" _
)
ActiveWorkbook.ShowPivotTableFieldList = True
'Debug.Print ptTracking.PivotFields(1).Name
'Returns nothing
'Add fields
With ptTracking
With .PivotFields(Date) 'Quotes here don't help :-/
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields(TechNbr)
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields(status)
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields(status)
.Orientation = xlDataField
.Position = 1
.caption = "TechTracker"
.Function = xlCount
End With
End With
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub
I've tried referencing the table range in many different ways, I've tried using hard references, and I've even tried declaring variables for each pivot field. Could anyone please help me correct the following code?
Date
是一种数据类型(用于日期),而不是其他PivotFields
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.