當您在工作簿上插入數據透視表時,Excel將緩存來自PivotTable.dataSource的數據。 (這就是爲什麼你需要更新您的數據透視表中包含源數據改變成透視表。)
數據緩存不足一半大的作爲工作表區域它的緩存,但顯著。 (在我的情況又增加了2.6MB到6.5MB存在,顯然一旦超過10MB,Excel啓動交換數據到磁盤)。
如果源數據是在一個單獨的表,你可以刪除源將您的數據僅保存在緩存中,並節省空間,而不是使用數據透視表破壞它。 (小心你did_not_uncheck Save data with the file
,雖然)
如果您稍後需要編輯數據源,你可以通過雙擊在數據透視表總計從緩存中的數據「通過鑽」。
閱讀Massive Excel Spreadsheets – How to Reduce the Size of Pivot Table Workbooks瞭解更多詳情。
作爲一個VBA程序員,我必須隱藏這一切都爲我的客戶,所以我寫了
代碼提取數據並刪除數據透視表
Public Sub unpackPivots()
On Error GoTo goneWrong
Dim PSheet As Worksheet, PTable As PivotTable, PBody As Range
For Each PSheet In ThisWorkbook.Sheets
If PSheet.Name Like "*Pivot" Then
Set PTable = PSheet.PivotTables(1)
PTable.ClearAllFilters
Set PBody = PTable.DataBodyRange
Set PBody = PBody.Cells(PBody.Rows.Count, PBody.Columns.Count)
PSheet.Activate
PBody.Select
PBody.ShowDetail = True
Application.ActiveSheet.Name = Replace(PSheet.Name, "Pivot", "_Data")
Application.DisplayAlerts = False
PSheet.Delete
End If
Next
Application.DisplayAlerts = True
Exit Sub
goneWrong: Application.DisplayAlerts = True Debug.Print Err.Number,Err.Description Debug.Assert False Err.Clear Resume End Sub
代碼來創建從數據表中的數據透視表和刪除表
Public Sub recreatePivots()
On Error GoTo goneWrong
Dim DSheet As Worksheet, DTable As ListObject
Dim PSheet As Worksheet, PCach As PivotCache
Dim PTable As PivotTable, PBody As Range
For Each DSheet In ThisWorkbook.Sheets
If DSheet.Name Like "*_Data" Then
Set PCache = ActiveWorkbook.PivotCaches.Create(_
SourceType:=xlDatabase, _
sourceData:=DSheet.ListObjects(1).Name, _
Version:=xlPivotTableVersion14 _
)
ThisWorkbook.Sheets.Add Before:=DSheet
Set PSheet = Application.ActiveSheet
PSheet.Name = Replace(DSheet.Name, "_Data", "_Pivot")
Set PTable = PCache.CreatePivotTable(_
TableDestination:=PSheet.Name + "!R3C1", _
DefaultVersion:=xlPivotTableVersion14 _
)
這裏說到您的業務相關的東西
Select Case DSheet.Name
Case "Stock_Data"
PTable.PivotFields("Month").Orientation = xlPageField
PTable.PivotFields("Manufacturer").Orientation = xlRowField
PTable.PivotFields("Warehouse").Orientation = xlColumnField
PTable.AddDataField PTable.PivotFields("Key"), _
"Nr of products", xlSum
Case Else
' Some other configuration of a pivote table
End Select
DSheet.Delete
End If
Next
Exit Sub
goneWrong:
Application.DisplayAlerts = True
Debug.Print Err.Number, Err.Description
Debug.Assert False
Err.Clear
Resume
End Sub