How would I use VBA to automatically populate an excel worksheet with data from the pivot table of a separate excel workbook? - vba

I have two open Excel workbooks. One has some data in a pivot table, like so:
Row Labels Date
A 5
B 4
C 3
The other, separate workbook is blank other than Column A, which lists the same Row Labels as are in the pivot table, but possibly in a different order:
Row Labels
B
A
C
I would like to place a button on the second workbook that, when pushed, will automatically populate the row labels with the appropriate columns from the pivot table, in the correct order.
I know that VLOOKUP will be involved somehow, but I am not quite sure how to get there. Ideally, it would be nice if this VBA code could be dynamic, so that the button would work no matter the size of the pivot table, and no matter how many row labels will be in the second workbook.

Use the GETPIVOTDATA function - for example:
=GETPIVOTDATA("Date",[PivotWorkbook]Sheet1!$A$1,"Row Field",$A2)
The workbook containing the Pivot Table must be open to calculate the result.

Related

Find number of rows in an already filtered Column A in Excel

I have got an Excel spreadsheet. This spreadsheet has just one tab in it. The Tab name is dynamic in nature and changes every week also the number of rows.
I have column A filtered already with a VBA macro. A1 has the header.
Now, I wanna find how many rows are there in this already filtered column A.
I am looking for any VBA function.
I have tried using Subtotal function.
=Subtotal(103,A2:A1345)
But I don't know the end range. As of now the end range is A1345. It will change every time in future if the new rows are added.
I tried multiple things but those did not work. I am quite new to VBA.
If A1 will never be blank, you could use (in a column other than A)
=Subtotal(103,A:A)-1.
Or, if there will be data below your table not to be counted, then format your table as a Table and use structured references (and this formula could go into column A)
=SUBTOTAL(103,Table1[column_header])
You can put the formula in column A if you use another column's last populated cell as the demarcation point.
If column B contains numbers then,
=subtotal(103, a2:index(a:a, match(1e99, b:b)))
If column B contains text then,
=subtotal(103, a2:index(a:a, match("zzz", b:b)))

Excel Autofilter, Copy selection, Paste to new sheet

I have a table with source data in columns Regions!A6:R553.
In Regions!A3:R3, I have formulas that pull specific information out of my data table in Regions!A6:R553 that I want copied to a different sheet.
Column A acts as my project name column, while column B holds ID numbers. In my case, there are multiple ID numbers per project.
I am looking for a script to filter and loop through all the unique order numbers in Column B one by one, then copy cells A3:R3 to RegionsSummary!A12:R12 for as many rows as there are unique order numbers (i.e, add rows to the table).
Here is a screengrab of my data sheet, "Regions":
i.stack.imgur.com/aTPuw.png
Here is a screengrab of the empty template sheet "RegionsSummary":
i.stack.imgur.com/9Ukz5.png
Example: Assume there are 5 projects in my data sheet. I will filter the data using another macro to select Project_1. I would then like a command button to active a macro that will filter to the first order number in Column B, copy Regions!A3:R3 to RegionsSummary!A12:R12, then filter to the second order number in Project_1, and repeat the process. This should go on until all unique ID numbers have been filtered and looped through.
Here is a screengrab of what a final product should look like:
i.stack.imgur.com/9Ukz5.png
Here is a link to the file: Final Output Example
I would go with an easier solution than a Macro with certain constraints. I am not able to access your sheet, so I will make a sample excel.
STEPS:
Create a list of unique projects for a dropdown (COPY Regions!A5:A10000 to a new sheet > Data > Remove Duplicates) . Create the dropdown (Data Validation > List > Select Range) using Data Validation in "Example_Result" sheet- C7.
In "Regions", in Col S, put the below formula
=S6&"_"&COUNTIF($S$6:S6,S6)
Copy this formula down for the entire sheet or as long as you expect the sheet to grow
In "Example_Result", insert an index column (1 to 1000, in Col A if you expect each project to have 1000 or less order numbers) from A12 onwards.
Along the columns (B onwards) of Row 11 include the names of the variables from Regions (Assessment Project, Highway etc).
Insert the below formula in B12 to S1000 (depending on number of variables) of Example_Result:
=IFERROR(INDEX(Regions!$A$5:$S$10000,MATCH($C$7&"_"&A$12,Regions!$S$5:$S$10000,0),MATCH(B$11,Regions!$A$5:$H$5,0)),"")

Macro or method to pivot data in Excel?

I am looking for a way to dynamically pivot data in an Excel sheet to put the data in a universal format no matter how wide the data set is.
For example: I want to be able to take a spreadsheet that could have 5 columns, 30 columns, or x number of columns to make it pivot to a universal format that is only three columns wide.
Here is a sample of how the original data could look:
I want it to look like this:
Is there a way to do it, either by macro or using any other Excel functions?
Assumes labels/data is in Sheet1 starting A1.
Insert a new ColumnA.
For Excel 2003: Activate any cell in your summary table and choose Data - PivotTable and PivotChart Report:
For later versions access the Wizard with Alt+D, P:
Select Multiple consolidation ranges and PivotTable and click Next >.
In “Step 2a of 3”, choose I will create the page fields and click Next >.
In “Step 2b of 3” (not shown in the modal!) specify your summary table range in the Range: field (A2:E6 for the sample data) and click Add, then Next >.
In “Step 3 of 3”, select a location for the PivotTable (the existing sheet should serve, as the PT is only required temporarily):
Click Finish to create the PivotTable:
Drill down (ie double-click) on the intersect of the Grand Totals (here Cell L4 or 16):
The PT may now be deleted.
In A2 of Table:
="Summary"&RIGHT(B2)
Convert the resulting Table into a conventional array of cells by selecting Table in the Quick Menu (right-click in the Table) and Convert to Range.
In a spare column starting in Row2 series fill integers down, from 1 to however many rows of data you started with (4, in the example). Then series fill that series to suit.
Sort sheet by that (spare) column, Smallest to Largest, with My data has headers checked.
Delete Row1 and spare column and hopefully you will end up with:

Copy rows with a specific value to another sheet

I have a huge Excel worksheet which contains 200,000 rows. The data is about 120 subjects which are listed in column B, like sinani-01 to sinani-120. I need to copy all rows where column B contains sinani-01, for example, and paste them to new sheet. Or at least just select them?
Can you please let me know how to do this in VBA?
Excel has an AutoFilter function that you can use to select only the rows that contain a specific value. You use it like this:
' Filter column B by a specific value...
Range("B:B").AutoFilter 1, "sinani-01"
' Copy all visible rows to a new sheet...
Range("B:B").SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("sinani-01").Range("A1")
' Turn the AutoFilter back off...
Range("B1").AutoFilter

Need to populate master list from many different sheets in excel

So lets say I have 3 different sheets each with information on them like this:
Sheet 1 Sheet2 Sheet 3
item1|2 item2|7 item1|4
item3|5 item3|6 item6|2
item9|7 item8|4 item7|8
The first part (code#) is a code for an item. The second number is the quantity of that item. What I need to do, is to be able to populate a master list which will pull the quantity of each item and sum them up. In the above case, the master list would look like this:
item1|6
item2|7
item3|11
item4|0
item5|0
item6|2
item7|8
item8|4
item9|7
I'm sorry if this is confusing but I really need some help!
Elaborating on #Skip Intro's solution:
Ensure all three sheets have the same labels (say Item and Qty), preferably in Row1.
Alt+D then P, select Multiple consolidation ranges and PivotTable, Next, Next.
Select your range from Sheet1 (say A:B), Add, repeat for Sheet2 and Sheet3, Finish.
Change Count of Value to Sum of Value, if required and might as well select PivotTable, PivotTable Options, Totals & Filters, untick (if necessary) Show grand totals for rows.
If you don't want (blank) to show either filter it out or restrict the data ranges (at the start) to less than whole columns.