Copy/Paste on new spreadsheet on same row

rach_oh

New Member
Joined
Aug 2, 2023
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top