I'm working on automating data analysis & creation of pivots and charts for which I have Raw Data (441379 Rows, 40 columns).
1) Raw Data changes monthly, so I need a dynamic range.
2) I have to create around 20 Pivot Tables with the same data.
3) I'm defining range and pivot cache for every single pivot. A lot of time is consumed in reading data and executing every single time.
4) I need the flexibility of choosing a range for placing the pivot table as one sheet may have more than one pivot table.
5) I need the flexibility of creating a sheet and naming them for pivot as I will have around 20 sheets.
How can I define pivot source and pivot cache once in the workbook so I can use the same for all the pivot tables?
Below is the code which I'm using
' For Dynamic data range named "PvtData"
ActiveWorkbook.Names.Add Name:="PvtData", RefersToR1C1:= _
"=OFFSET('Data'!R1C1,0,0,COUNTA('Data'!C1),COUNTA('Data'!R1))"
' For creating Pivot using Dynamic data range named "PvtData"
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"PvtData").CreatePivotTable TableDestination:="", TableName:="PivotTable_UBD"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(7, 1)
ActiveSheet.Cells(7, 1).Select
ActiveSheet.PivotTables("PivotTable_UBD").SmallGrid = False
ActiveSheet.Name = "Utilization By Day"
Or
' For Dynamic data range named "PvtData"
ActiveWorkbook.Names.Add Name:="PvtData", RefersToR1C1:= _
"=OFFSET('Data'!R1C1,0,0,COUNTA('Data'!C1),COUNTA('Data'!R1))"
' For creating Pivot using Dynamic data range named "PvtData"
Set PivotCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="PvtData")
Worksheets.Add
ActiveSheet.Name = "Utilization By Day"
ActiveWindow.DisplayGridlines = True
Set PivotTable = ActiveSheet.PivotTables.Add(PivotCache:=PivotCache, _
TableDestination:=Range("A1"), TableName:="PivotTable_UBD")
I know this has been here a while and not sure you still need it answered.
Take your Raw data and turn it into a table.
Create a Pivot Table from the data source that is in table format. Once created from the table your pivot source becomes a dynamic source. As the table size changes the pivot source changes since it is referencing the table.
Copy the pivot table and paste the pivot table 19 times. Since they are all copies, they work from one cache. Once one pivot is refreshed all 20 are refreshed regardless of how many sheets they are on.
Related
i'm new to both macros and vba
I have a pivot table in sheet FR Pivot and a table in sheet Final. I want to pick up the pivot table data and use vlookup to fill in values in my table in the Final sheet. The issue is that the pivot table can be dynamic. So I'm trying the following:
Dim Range1 As Range
With Sheets("FR Pivot")
'Select cell - You can change as per your requirement
.Range("E4").Select
'Selects the current region
Selection.CurrentRegion.Select
Set Range1 = Selection
End With
Sheets("Final").Select
Range("J7").Select
ActiveCell.FormulaR1C1 = _
"=IFNA(VLOOKUP([#[Row Labels]],Range1,2,0),""NA"")"
I think I'm going wrong with the named range part but unable to figure out how to fix it. For the macro, I'm recording my actions and then modifying code to make it dynamic which is why there are so many selects.
Without any selections:
Dim addr
addr = Sheets("FR Pivot").Range("E4").Currentregion.Address()
Sheets("Final").Range("J7").Formula = _
"=IFNA(VLOOKUP([#[Row Labels]], 'FR Pivot'!" & addr & ",2,0),""NA"")"
I have a spreadsheet in Excel with three pivot charts. I wrote a VBA code that does the following:
' Copies sheets 1-4
ThisWorkbook.Worksheets(Array("1", "2", "3", "4")).Copy
'Transforms each new sheet data in values only.
For Each iSheet In ActiveWorkbook.Sheets
iSheet.Activate
iSheet.UsedRange.Copy
iSheet.UsedRange.PasteSpecial xlPasteValuesAndNumberFormats
iSheet.Range("A1").Select
Next iSheet
' After pasting, deletes the contents of the cells but keeps the graphs
' in spread sheet 4
ActiveWorkbook.Sheets("4").Columns("J:AB").Clear
This code runs differently in debug mode (F8) compared to when it is run by pressing F5 or running with a button in Excel. In debug mode, it keeps all the series information in the Pivot Charts when the Pivot Tables are pasted as values. When it runs nonstop, it loses the pivot charts information, so that for instance the series names change to defaults 'Series 1', the x values go from specific date values to jan-00, format of x axis labels change to text, etc. I have no idea why. In other very similar question, someone pointed out that PasteSpecial behaves differently when the target worksheet is not activated, which is why I added iSheet.Activate. This difference in action may also be a result of the Clear method of the Pivot Tables.
I tried to add Activate in different steps in the code to no avail.
I'm assuming you want to leave only values on every sheet on the active workbook so this is it:
' Copies sheets 1-4
ThisWorkbook.Worksheets(Array("1", "2", "3", "4")).Copy
'Transforms each new sheet data in values only.
For Each iSheet In ActiveWorkbook.Sheets
iSheet.UsedRange.Values = iSheet.UsedRange.Values
Next iSheet
Thought the first line copying the sheets has no sense.
Looks like you don't care if you really keep the values of the pivot table. In an example I just ran, I have a pivot table in G2:L19. I executed this line of code, which deleted the columns containing the pivot table, and the pivot chart remained, plotting the same data, but the data was now hard-coded into the chart.
ActiveSheet.Range("G:L").Delete
If you do want to retain the values and number formats but not the pivot table itself, this code, run in F5 or F8 mode, keeps the data where the pivot table was, and it keeps the data in the chart, hard-coded (unlinked from the pivot table).
Sub UnpivotTable()
With ActiveSheet.Range("G2:L19")
.Copy
.PasteSpecial xlPasteValuesAndNumberFormats
End With
End Sub
Do you want to keep the links from the chart to the data? Store the series formulas, undo the pivot table, and reapply the series formulas:
Sub UnpivotTableKeepChartLinks()
Dim MyChart As Chart
Set MyChart = ActiveSheet.ChartObjects("Chart 1").Chart
Dim vSrsFmlas As Variant
ReDim vSrsFmlas(1 To MyChart.SeriesCollection.Count)
Dim iSrs As Long
For iSrs = 1 To MyChart.SeriesCollection.Count
vSrsFmlas(iSrs) = MyChart.SeriesCollection(iSrs).Formula
Next
With ActiveSheet.Range("G2:L19")
.Copy
.PasteSpecial xlPasteValuesAndNumberFormats
End With
For iSrs = 1 To MyChart.SeriesCollection.Count
MyChart.SeriesCollection(iSrs).Formula = vSrsFmlas(iSrs)
Next
End Sub
I have three rows of data placed just after a pivot table. I am trying to create a single process to cut that data, prior to a pivot table refresh and then paste after the pivot table has been refreshed. The pivot table occupies columns B-I, and the cells occupy the same columns. I have one row of separation between the end of the pivot table and the three rows of data.
Well, this is one of those things that's tricky because you need to know how big the pivot will get after the refresh, but you need to know it before it refreshes.
I think you might as well just insert a bunch of rows between your data and the pivot, refresh the pivot safely, and then remove the extra rows.
Sub RefreshButPreserveFooterRows()
Dim pt As PivotTable
Dim PivotOriginalEndPosition As Integer
Dim PivotRefreshedEndPosition As Integer
Dim safetyGap
'pick how many rows you want to insert. Might as well be generous.
safetyGap = 100
Set pt = ActiveSheet.PivotTables("PivotTable1")
'Deterimine the position of the first row after your pivot table prior to refresh
PivotOriginalEndPosition = pt.RowRange.Rows.Count + pt.RowRange.Row
'Insert howerver many rows you specified as "Safety Gap" between your pivot and your data
Rows(PivotOriginalEndPosition & ":" & PivotOriginalEndPosition + safetyGap).Insert Shift:=xlDown
'refresh the pivot
pt.RefreshTable
'Deterimine the position of the first row after your pivot table after to refresh
PivotRefreshedEndPosition = pt.RowRange.Rows.Count + pt.RowRange.Row
'delete the extra rows from the safety gap, accounting for the new size of the pivot table
Rows(PivotRefreshedEndPosition & ":" & PivotOriginalEndPosition + safetyGap).Delete
End Sub
I have an Excel document which pulls data from an Access database.
From here, the data is summarised in a Pivot Table.
It is then pulled into another table which makes it easier to read and filter.
The last table is formatted as a table and formulas are in place.
Depending on the data in the database, this table can reduce or expand the number of rows when refreshed.
When I run the macro to refresh the data and tables, I want to be able to automatically resize the table so all of the data is shown, but no extra blank rows appear at the bottom.
So far, I have the following code which looks up the Pivot Table sheet (Pivot) to determine the number of rows to display in the output sheet (Report):
Sub ResizeList()
Dim ws As Worksheet
Dim ob As ListObject
Dim Lrow1 As Long
Lrow1 = Sheets("Pivot").Cells(Rows.Count, "A").End(xlUp).Row
Set ws = ActiveWorkbook.Worksheets("Report")
Set ob = ws.ListObjects("Report_Table")
ob.Resize ob.Range.Resize(Lrow1)
End Sub
However, it only removes the table formatting (not the data) from the extra rows at the bottom when the table reduces in size.
Also, I get too many rows in the Report Table because of the header and total rows in the pivot.
Can someone help?
I have multiple pivot charts each with their own pivot table on separate worksheets in Excel 2002.
When I try to generate these charts with VBA with the following code:
Set cht = Charts.Add(After:=Worksheets("Setup"))
With cht
' we use named ranges here
.SetSourceData Source:=range(tblName)
.Name = chtName
....
where tblName is a named range just created a few lines before, the code runs fine if there is only one table and chart generate but gives me a run time error 1004: "The source data of a PivotChart report cannot be changed..." if I try to generate pivot table and chart set one after another.
Going to Insert -> Name -> Define, the list of named ranges created seems to be correct.
What is the correct way of setting the source data for a pivot chart with a dynamic range?
I think you might be trying to do too many things at once.
If the Data Source is going to change, I wouldn't use a Pivot Chart.
Use a pivot table, create the chart at runtime (like your example). Build your chart of the results of the pivot table.
This piece of code assumes that you have only one pivot table per sheet and the pivot table starts on Cell A1:
Sheets(wsName).Select
Range("A1").Select
Set cht = Charts.Add(after:=Worksheets(Worksheets.Count))
With cht
.SetSourceData Sheets(wsName).Range("A1")
.Name = chtName
...
Also changing "Worksheets.Count" to a specific worksheet name seems to trigger that error as well.