TheRedCardinal
Active Member
- Joined
- Jul 11, 2019
- Messages
- 252
- Office Version
- 365
- 2021
- Platform
- Windows
My spreadsheet works as follows:
I think I remember when I first started doing pivot tables that you need to manage the data cache but I never really needed to make more than one at a time before so it's never been an issue.
The headers in each sheet are the same.
Here is my code:
OperateMonths is my collection successfully created in the previous steps.
- There's a list of worksheets loaded into a collection
- The macro cycles through each worksheet in that collection
- It selects all the data in the sheet (UsedRange) then puts a pivot table in a certain column at the bottom of that table
- The pivot table is the same in each sheet
I think I remember when I first started doing pivot tables that you need to manage the data cache but I never really needed to make more than one at a time before so it's never been an issue.
The headers in each sheet are the same.
Here is my code:
VBA Code:
For Counter = 1 To OperateMonths.Count
Set WS2 = WBk.Sheets(Range("RegLetter") & " - " & OperateMonths(Counter))
With WS2
LRow = .Cells(Rows.Count, 2).End(xlUp).Row
Set PivotData = WS2.UsedRange
WBk.Names.Add Name:="DataforPivot", RefersTo:=PivotData
End With
'Create Pivot Table
Dim PivTotal As Range, PivData As Range, Source As PivotCache, Table As PivotTable
Dim PCache As PivotCache, PTable As PivotTable, PRange As Range, PFields As PivotField
Dim InsertRows As String
Dim PasteRange As Range
Dim GTotal As Long
On Error Resume Next
Application.DisplayAlerts = False
'Define Data Range
Set PRange = Range("DataforPivot")
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=WS2.Cells(LRow + 2, 7), TableName:=OperateMonths(Counter))
Set PTable = PCache.CreatePivotTable(TableDestination:=WS2.Cells(LRow + 2, 7), TableName:=OperateMonths(Counter))
'Insert Row Fields -
With ActiveSheet.PivotTables(OperateMonths(Counter)).PivotFields("Tax Code")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables(OperateMonths(Counter)).PivotFields("Tax Code Description")
.Orientation = xlRowField
.Position = 2
End With
'Insert Data Fields
With ActiveSheet.PivotTables(OperateMonths(Counter)).PivotFields("Net")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0.00"
.Name = "Total Value"
End With
With ActiveSheet.PivotTables(OperateMonths(Counter)).PivotFields("VAT")
.Orientation = xlDataField
.Position = 2
.Function = xlSum
.NumberFormat = "#,##0.00"
.Name = "Total Value"
End With
'Format Pivot Table
With ActiveSheet.PivotTables(OperateMonths(Counter))
.InGridDropZones = True
.RowAxisLayout xlTabularRow
.ShowTableStyleRowStripes = True
.TableStyle2 = "PivotStyleLight2"
For Each PFields In .PivotFields
PFields.Subtotals(1) = False
PFields.RepeatLabels = True
Next PFields
End With
Application.DisplayAlerts = True
Next Counter
OperateMonths is my collection successfully created in the previous steps.