Using VBA to copy data from pivot and paste into defined ranges on another sheet. Idea is to copy all columns in pivot for each change in first pivot

JmanRueng

New Member
Joined
Jan 19, 2010
Messages
7
Hi All, (sorry for wordy post)

A VBA newby, but I've tasted the CoolAid and I know how powerful it can be so I'm trying to improve my skills.

In short, I'm trying to create a process to pull select data from a pivot table and copy it to a specific range on another sheet.

The pivot table has 10 columns (7 row labels, 3 values) and the first column row label is title "Strategy". I've setup the pivot so that the top 10 projects are displayed for a given strategy, however sometimes there aren't 10 projects per strategy but instead only 1, or 3, or 5, for example...

There are 36 Strategies in the Pivot.


Ideally what I'd like to do is automate a process that copies - for every change in the 1st column (strategy) - the nine columns to the right, and populates 36 corresponding 9X10 tables/named ranges on another excel tab.

I know how to copy an entire pivot and paste into a specific location using VBA, but I wanted to avoid making 36 pivot tables if I could. Any help would be appreciated!

I mocked up a workbook as well, if easier (although I'm not sure how to share this!)

Thanks!

Example below...Strategy 1 has 12 (but I only want first 10), Strategy 10 has 4 rows, and Strategy 11 has 9 rows. I would want the macro to for every change in strategy copy the data and paste in corresponding data table (named by the same strategy) on another sheet.

StrategyProject #Project NameGBUPillarLeadEval ScoreSum of BP CapitalSum of LE CapitalSum of Act Capital
Strategy 170Name 70GBU 1SourceJoe3$985,000$985,000$985,000
Strategy 134Name 34GBU 1SourceMark3$499,000$499,000$499,000
Strategy 1268Name 268GBU 1SourceMark3$499,000$499,000$499,000
Strategy 1202Name 202GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 1160Name 160GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 1175Name 175GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 1189Name 189GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 193Name 93GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 1235Name 235GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 1113Name 113GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 11Name 1GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 1132Name 132GBU 1PlanJohn0$50,000$50,000$50,000
Strategy 10241Name 241GBU 1MakeSally0$134,500$134,500$134,500
Strategy 10208Name 208GBU 1MakeSally0$134,500$134,500$134,500
Strategy 107Name 7GBU 1MakeSally0$134,500$134,500$134,500
Strategy 10195Name 195GBU 1MakeSally0$134,500$134,500$134,500
Strategy 1180Name 80GBU 2DeliverJoe1$1,120,000$1,120,000$1,120,000
Strategy 11182Name 182GBU 2DeliverSteve1$148,000$148,000$148,000
Strategy 11209Name 209GBU 2DeliverSteve1$148,000$148,000$148,000
Strategy 11100Name 100GBU 2DeliverSteve1$148,000$148,000$148,000
Strategy 11196Name 196GBU 2DeliverSteve1$148,000$148,000$148,000
Strategy 11120Name 120GBU 2DeliverSteve1$148,000$148,000$148,000
Strategy 11242Name 242GBU 2DeliverSteve1$148,000$148,000$148,000
Strategy 11139Name 139GBU 2DeliverSteve1$148,000$148,000$148,000
Strategy 11167Name 167GBU 2DeliverSteve1$148,000$148,000$148,000

<colgroup><col><col><col><col span="2"><col span="2"><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
A VBA newby, but I've tasted the CoolAid and I know how powerful it can be so I'm trying to improve my skills.

:LOL: Be careful, this CoolAid can be addicting.

Stepping back to look at the big picture, it might be more direct to just pull your data directly from your data source to the 36 tables instead of having the intermediate Pivot.

Is there any reason that you need or want the Pivot? For example, if you need to dynamically change other filters through the Pivot and have those changes reflected in the tables, that might be good interface; however if there are no filters or static filters, then you could go right to the tables.
 
Upvote 0
Hi JS411, Interesting suggestion. My rawdata is significantly more complex - 163 columns with 37 additional formula columns, and over 8000 rows of data. That said, I could easily write a macro to copy the pivot and paste it as values on another sheet so my "new raw data" would be exactly the information that I need. The last step would be writing some code to copy columns/rows of data for every change in the first column strategy and then to paste in corresponding strategy named range...any thoughts?
 
Upvote 0
I'm still interested to see what everyone says, but I found one potential solution (partial) from another thread. The idea is to use the autofilter function to copy.

Sub FilterCopy()
Application.ScreenUpdating = False


[allrawdata].AutoFilter Field:=1, Criteria1:="x"
[selectedrawdatacolumns].Copy Sheets("DataTable").[StrategyTable1]
[A1:C10].AutoFilter
Application.ScreenUpdating = True
End Sub

This will move the right data to the right location, but I have one snare - it moves all data. I only want to move 10 rows worth...the first ten. And even though I'm using a top 10 filter, sometimes there are more than 10 rows...when there's a tie based on filter value. Any suggestions for this (or other more efficient approaches)?

I could write 36 segments of the same code for each strategy...assuming i fix the top 10 issue.
 
Upvote 0
When I suggested pulling your data directly from the Raw Data to the 36 reports I was thinking of using querries. That can be done using Query Tables with MS Query or ADO through VBA code.

That would be similar the approach you were considering of applying an AutoFilter to the Raw Data and copying Visible Records. A key difference would be that with a Query you can Aggregate (Sum, Count, Average) your data, which can't be done by AutoFilter-Copy-Paste alone.
If you are listing the Top 10 projects and each project has only 1 unique record, then Aggregating isn't required..but even then a query is a good approach to consider.

A Query would be made using SQL which would allow you to apply a TOP 10 clause to filter out only the Top 10 aggregated results.
The TOP 10 would return no more than 10 items. If there are ties, the query simply truncates the items greater than the Top 10.

I'd be glad to help if you'd like to pursue that approach.
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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