For instance I have a pivot table on every third worksheet that needs to be edited, on the worksheet is 3 pivot tables, I don't actually know the name of the table but I know it lies on $N$2, is there anyway to get the name or handle of the pivot table from the cell?
The Range object has a PivotTable property that returns the object you want
Set pt = MySheet.Range("N2").PivotTable
Related
I have a combo box with a list of names, that filters a pivot table.
Each name I select changes the values shown at the pivot table.
I need a code that populates a second combo box with those pivot tables values. When I choose a name on the first combo box, the options at the second one will be the ones filtered by that name on the pivot table.
I have done that before in a simpler manner with fixed tables, but now the values are going to change constantly, and that's why I need to use a pivot table for that.
You can use the DataRange of your PivotField as source for your ComboBox.
You may either add each cell's value to the ComboBox items ...
Dim aCell As Range
For Each aCell In ActiveSheet.PivotTables(1).RowFields(1).DataRange.Cells
ActiveSheet.ComboBox1.AddItem aCell.Value
Next aCell
... or the whole ComboBox.List in one by a simple assignment:
ActiveSheet.ComboBox1.List = ActiveSheet.PivotTables(1).RowFields(1).DataRange.Value
I guess you used your "TIMBRA..."-list as the only row field in your pivot table.
If not, you can address the DataRange by names instead, e. g.
ActiveWorkbook.Sheets("...").PivotTables("...").PivotFields("...").DataRange
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 a Excel Pivot table linked my Word report.
The Pivot table will growth when getting more data. However, when the Pivot table size change, it doesn't be reflected in the Word file. The word file always display the original selected range. So I have to update the link range manually.
Is there is a way to fix this issue or simply the effort?
Many thanks.
Below is my solution to fix this issue.
1. Create a Named Range for the Pivot Table.
2. Change the Linked Range absolute address in Word file to the Named Range
================
Note for 1:
create named range for a Pivot table can be done via Excel "Offset" function.
However, the Offset function is not perfect when there are more data in the same sheet.
so I create my own Excel function to that.
Function PVRange1(Sheet_Name, Pivot_Name) As Range
'Returns a Range object that represents the range containing the entire PivotTable report, but doesn’t include page fields.
'
Dim pvt As PivotTable
Set pvt = Worksheets(Sheet_Name).PivotTables(Pivot_Name)
Set PVRange1 = pvt.TableRange1
End Function
Function PVRange2(Sheet_Name, Pivot_Name) As Range
'Returns a Range object that represents the range containing the entire PivotTable report, including page fields.
Dim pvt As PivotTable
Set pvt = Worksheets(Sheet_Name).PivotTables(Pivot_Name)
Set PVRange2 = pvt.TableRange2
End Function
In Excel 2003, I have a macro that accidentally tried selecting a page field item (New York) that didn't exist. This renamed the currently selected page field (Alabama) to the value I tried to select (New York).
I now have 3 or 4 values that are now wrong. Is there a way to refresh these pivot table values to the correct values without recreating the pivot table from scratch?
Thanks
Use this code -
Sub Clean_AllPivotsOnSheet()
Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
Next pt
End Sub
Then refresh the Pivot Table.
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.