Loop to copy paste pivot table data changing the filter every time and copy it

MPaul100

New Member
Joined
Aug 21, 2019
Messages
24
I have a strange one. I am looking to copy only the values from a pivot table into specific cells in another workbook, by changing the filter everytime. Here is my code so far, the copy part is out of my league and I am hopping that someone has an idea about this. Also, if the pivot for a filter choice is blank, can it be skipped?

File-Copy-icon.png

<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Option Explicit

Sub FilterPivotTable()

Dim rLastCell As Range
Dim PvtTbl As PivotTable
Dim ws1 As Worksheet, ws2 As Worksheet
Dim wb2 As Workbook

Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set wb2 = Workbooks.Open("filepath")
Set ws2 = wb2.Sheets("EX")

Dim rowCount As Long
Dim LastRow1 As Long
Dim pvtField As PivotField

Set PvtTbl = ws2.PivotTables("PivotTable2")

Application.ScreenUpdating = False

Set pvtField = PvtTbl.PivotFields("Divisions") 'extend etc as required

Dim myArr()
myArr = Array("Rail1", "Rail2", "RailC", "RailM", "Airborn", "Ret")

'PvtTbl.ManualUpdate = False

Dim i As Long

For i = LBound(myArr) To UBound(myArr)

pvtField.ClearAllFilters
pvtField.PivotFilters. _
Add Type:=xlCaptionContains, Value1:=myArr(i)

With ws1
'how can I copy the data in specific cell for each pivot filter change? In my case: D7, D23, D55, D66, D81, D106
End With

Next i

Application.ScreenUpdating = True
'PvtTbl.ManualUpdate = False
End Sub</code>

Cross-post: https://www.excelforum.com/excel-pr...anging-the-filter-every-time-and-copy-it.html
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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