Trying to loop through each pivotRow of each of the pivotTables & copy over (3) pivot fields' data to 12 new rows in a table on a new sheet. (keeping the correlating pivot fields on each pivot row on the same row in the table). I've gotten the code to copy over some values but they are not the active values shown on the pivottables and the data in each column isn't on the right rows to match the pivotTable. Any suggestions???
VBA Code:
Sub Test()
Application.ScreenUpdating = False
' Destination Table
Dim tbl As ListObject
Set tbl = Worksheets("Dynamics Budget Upload").ListObjects("DynamicsBudgetUpload")
' PivotTables with Data
Dim PivotTables(0 To 3) As PivotTable
Set PivotTables(0) = Worksheets("Golf Pivot").PivotTables("Golf Pivot")
Set PivotTables(1) = Worksheets("Maintenance Pivot").PivotTables("Maintenance Pivot")
Set PivotTables(2) = Worksheets("F&B Pivot").PivotTables("F&B Pivot")
Set PivotTables(3) = Worksheets("G&A Pivot").PivotTables("G&A Pivot")
' Mapping of Pivot Field Names to Table Column Names
Dim fieldMapping As Object
Set fieldMapping = CreateObject("Scripting.Dictionary")
fieldMapping("Accounting Structure") = "Accounting Structure"
fieldMapping("Dimension Values") = "Dimension Values"
fieldMapping("Amount Type") = "Amount Type"
' Add more mappings as needed
' Start Sub
MsgBox "Budget Upload Entry being created"
ActiveWorkbook.RefreshAll
Dim ptIndex As Long
Dim pt As PivotTable
Dim pivotField As PivotField
Dim newRow As ListRow
Dim columnIndex As Long
Dim pivotItem As PivotItem
For ptIndex = LBound(PivotTables) To UBound(PivotTables)
Set pt = PivotTables(ptIndex)
For Each pivotField In pt.PivotFields
If fieldMapping.Exists(pivotField.Name) Then
columnIndex = tbl.ListColumns(fieldMapping(pivotField.Name)).Index
For Each pivotItem In pivotField.PivotItems
For i = 1 To 12 ' Copy each pivot row 12 times
Set newRow = tbl.ListRows.Add
newRow.Range(1, columnIndex).Value = pivotItem.Caption
Next i
Next pivotItem
End If
Next pivotField
' Transpose Date Range
Dim rngList As Range
Set rngList = Worksheets("Golf Pivot").Range("D5:O5")
rngList.Copy
tbl.ListColumns("Date").DataBodyRange.PasteSpecial Paste:=xlPasteValues, Transpose:=True
Next ptIndex
Application.ScreenUpdating = True
MsgBox "Complete"
End Sub