Using VBA to Automatically Filter and Pull Data from a Pivot Table

BambooRoot

New Member
Joined
Jul 19, 2012
Messages
6
Hi guys,

I'm loving this forum so far, I've always subscribed to the beleif that if you're trying to learn any new skill, surround yourself with as many 'gurus' as possible. You've all been a huge help.

So for everyday purposes I'd consider myself an 'intermediate' Excel user. I'm looking to u
pgrade my skillset slightly by incorporating VBA. I’ve been playing with the macro editor, but I’m currently stuck on the following:
<o:p></o:p><o:p> </o:p><o:p></o:p>
<o:p></o:p>
<o:p></o:p>
I have a pivot table located in Workbook1. My task is to filter the data in this table, and cut and paste the results into another workbook, Workbook2. I’m wondering if there is a way to automate the entire pivot-table filter process, such that the code applies the filters, pulls the required data, and pastes it into the appropriate rows. If anyone could point me in the right direction (don’t write out the entire code as I’d like to figure it out eventually) that would be great.
<o:p></o:p>
<o:p> </o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Thanks in advance,
<o:p></o:p>
Steve
<o:p></o:p>
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Steve,

The code for the filtering step will depend on some details.
1. To which areas of the PivotTable are you applying the filter (Report Filters, Row Labels, Column Labels or Data Values)?
2. Are you filtering to show a single item, mulitiple items in a list, or a criteria-based filter like (dates after 1/1/13, or sales > 10,000)?
3. Will the source for the filter criteria be written into the VBA code, or read from somewhere like a range or data validation cell?

Regarding the copy part, you can get a good start using the Macro recorder; however it will record a specific range address that was copied.
To make that more dynamic, look in the VBA Help documentation to understand how to reference different parts of a PivotTable using its properties.
If your PivotTable object is assigned to the variable PT, you can use references like: PT.TableRange1, PT.DataBodyRange to copy the parts that you want.
 
Last edited:
Upvote 0
Hi Jerry, thanks for the reply!

So I've managed to work out the code over a lengthy VBA session last night. The one thing that I am still stuck on though is this:

The report that I'm populating is a monthly one. Each month one additional row is added to the report, meaning the range that I'm populating will shift. So far my query will only populate the range I initially designate, meaning next month, it will pull the numbers from Workbook2 and paste them over the numbers from this month.

I imagine I can use either the match function (i.e. to match the appropriate months with each other) or an offset function, but I can't figure out how to code either of these. Do I place the formula at the beginning of the code? Or somewhere in the middle?
 
Upvote 0
Steve, If I'm understanding you correctly, you can use code like this to find the last row of data in Column B, then paste your PivotTable data for the current month in the blank row below it.

Code:
With Workbooks("Workbook1").Sheets("Summary")
    .Cells(.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial (xlPasteValues)
End With
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,883
Members
449,477
Latest member
panjongshing

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