我是VBA编程新手,目前为几个xlsx / xls文件创建数据透视表。有些文件有超过70,000个实例。
'Create Pivot Table for PAX Traffic for each subclasses
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Dim SourceRange As Range
Set SourceRange = Selection
Application.CutCopyMode = False
Sheets.Add
ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SourceRange, _
Version:=xlPivotTableVersion11).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion11
直到具有超过70,000个实例的xlsx文件才能正常工作。
感谢您的温和帮助。
答案 0 :(得分:1)
试试这个
Sub CreatePivot()
Dim pt1 As PivotTable
Dim ptcache1 As PivotCache
Dim trange As Range
Dim srange As Range
Set srange = ActiveSheet.Range("A1").CurrentRegion
Set trange = Sheets("Sheet2").Range("A1")
Set ptcache1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, srange)
Set pt1 = ptcache1.CreatePivotTable(trange, "do_Amazing")
With pt1
.PivotFields("...PivotFields1...").Orientation = xlRowField
.PivotFields("...PivotFields2...").Orientation = xlDataField
End With
答案 1 :(得分:1)
我能够通过将Range转换为Excel表(在VBA中称为ListObject)来解决此问题。
然后,我将Excel表而不是Range作为PivotCaches.Create()
的源。
它刚刚起作用。