![](/img/trans.png)
[英]How can I place my data fields in COLUMNS in my Excel Pivot Table (Aspose Cells)
[英]How can I reference pivot fields from excel table?
我正在尝试编写将通过Excel表创建数据透视表的代码,并且不断收到运行时错误,这些错误指控我无效的数据透视表字段名称。
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
我尝试以多种不同方式引用表范围,尝试使用硬引用,甚至尝试为每个数据透视表字段声明变量。 谁能帮我纠正以下代码?
Date
是一种数据类型(用于日期),而不是其他PivotFields
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.