In my active sheet I have two Pivot table (PivotTable1 and PivotTable2) sometimes they could be more though.
I am trying to add a chart for each PT in the active sheet. So far I came up with this code but it is giving me 4 charts 3 blank ones and a correct one if one of the PT is selected.
Sub CreateChart1()
Dim pivot As PivotTable, sh As Worksheet, nrp As Long
Set sh = ThisWorkbook.Sheets("Sheet1")
For nrp = 1 To sh.PivotTables.Count
Set pivot = ActiveSheet.PivotTables(nrp)
For Each pivot In sh.PivotTables
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=pivot.Parent.Name
Next pivot
Next nrp
End Sub
Can somebody explain to me what am I doing wrong?
Any help is appreciated.
Thanks
Nick.
You are getting four charts even though you only have two pivot tables because you have two loops and only need one.
The inner loop For Each pivot In sh.PivotTables loops through each pivot table, as does the outer loop For nrp = 1 To sh.PivotTables.Count. So you'll get 2 charts for each pivot table.
Instead of using Charts.Add I suggest using ChartObjects.Add, which creates an embedded chart and where you can control the placement and size.
Then you also need to SetSourceData.
An example might look like this:
Sub AddPivotChart()
Dim pivotTbl As PivotTable, ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
For Each pivotTbl In ws.PivotTables
Dim chtObj As ChartObject
Set chtObj = ws.ChartObjects.Add(50, 50, 200, 200) ' adjust as needed
chtObj.Name = pivotTbl.Name
chtObj.Chart.SetSourceData pivotTbl.TableRange1
Next pivotTbl
End Sub
Related
I want to change automatically the range of the sourcedata of each pivot table of each worksheet. I have the sheet 'DATA' which feeds every pivot table of the workbook. The point is that the range of the DATA sheet is variable. So when I change the DATA sheet I want to refresh all the pivot table adjusting also the rnew range of data.
I wrote the following script, but it is not working. I don't know why:
Sub Prueba_Rango_TD()
Dim ws As Worksheet
Dim LastRow As Integer
LastRow = Cells(Rows.Count, 3).End(xlUp).Row
For Each ws In ThisWorkbook.Worksheets
For Each tbl In ws.ListObjects
tbl.Resize tbl.Range.Resize("DATA!B8:O" & Ultima_fila)
Next tbl
Next ws
End Sub
Can you help me please?
Thank you!
The easy way you do this is by recording a Macro.
Then you will se what type of Objects being used and where the pointer of your first Pivot, something like this:
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache
ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=ThisWorkbook.Worksheets("DATA").UsedRange.Address,
Version:=xlPivotTableVersion12)
If all the other sheets points towards the same data Cashe you do it with the loop.
.PivotTables("PivotTable1").ChangePivotCache ("DATA!PivotTable1")
.PivotTables("PivotTable1").RefreshTable
I have a table in Sheet 2 with a name "MyTable". Number of rows of that table changes each time depending on the data. I would like to clear the contents of the table and resize it using a macro so that it has only two rows- a title row, and an empty row.
Table title row is from B5 until K5.
I tried the below code, it clears the table contents and resizes, however, does not resize as desired. It resizes, without clearing the table borders in column C.
Any help is really appreciated.
Sub Table_Resize()
Dim rng as Range
Sheet2.Select
Range("MyTable").ClearContents
Set rng = Range("MyTable[#All]").Resize(2, 10)
Sheet2.ListObjects("MyTable").Resize rng
End Sub
I think that what you are trying to do is to delete the all rows.
Sub Table_ClearContents_Resize()
Dim ws As Worksheet: Set ws = Sheets("Sheet2")
Dim ol As ListObject: Set ol = ws.ListObjects("MyTable")
' Delete table contents
ol.DataBodyRange.ClearContents
' Resize table
ol.Resize Range(ol.HeaderRowRange.Resize(2).Address)
End Sub
I am hoping someone can help me figure out a really annoying aesthetic feature of pivot tables in Excel.
Here is what my current "analysis" screen looks like without the pivot tables:
However, I have placed pivot tables in the black boxes and have a macro to auto update them after I import more data. That is below:
Sub UpdatePivots()
' This sub is intended to update all pivot charts in the by switching to the appropriate
' worksheet, locating the appropriate pivot table, and updating them.
Dim ws As Worksheet
Dim PT As PivotTable
Dim pvtItem As PivotItem
For Each ws In ActiveWorkbook.Worksheets '<~~ Loop all worksheets in workbook
For Each PT In ws.PivotTables '<~~ Loop all pivot tables in worksheet
PT.PivotCache.Refresh
Next PT
Next ws
End Sub
However, the imported data changes. Sometimes it is very small, sometimes it is very large, and so I end up with my pivot tables looking like this:
How is it possible for me to either write code, or change settings, so those ugly white rows don't appear? I would like for the gray to remain if there are no pivot rows. I have tried checking in Pivot Table formatting settings but have been unsuccessful.
You can use a standard last row identifier and then color your interior cells below. You will need to pick a max row number (here it always color form last row down to 100, so change 100 if needed). You will also need to set the columns since you have a black border.
Last, you will just need to add the color code. I added a bit of code below to extract the color code if you don't know what it already.
This solution will work for the left pivot. You will need to duplicate the two lines of code I added for the pivot on right.
Sub UpdatePivots()
Dim ws As Worksheet
Dim PT As PivotTable, pvtItem As PivotItem
Dim LRow As Long
For Each ws In ActiveWorkbook.Worksheets
For Each PT In ws.PivotTables
PT.PivotCache.Refresh
LRow = ws.Range("D" & ws.Rows.Count).End(xlUp).Offset(1).Row
ws.Range(ws.Cells(LRow, "D"), ws.Cells(100, "G")).Interior.Color = 11184814
Next PT
Next ws
End Sub
If you do not know the color code, you can run the below code to find it!
Sub ColorCode()
Dim Target As Range
Set Target = Application.InputBox("Select desired cell to return color code", Type:=8)
If Not Target.Count > 1 Then
MsgBox "Color Code: " & Target.Interior.Color
End If
End Sub
I trying to write a macro to format a number of sheets containing pivot tables. I am stuck on this one problem.
My sheet contains a number of stacked pivot tables. I need the VBA code snippet to hide, say, 1000 rows after the first table (or all tables except the top one if possible). The top table will vary in size from one day to the next so it is not possible to just hide a set range.
Thanks
To hide all the pivot tables except one, you may try something like this...
In the below code, change the name of the pivot table you don't want to hide.
Sub HideAllPivotTablesButOne()
Dim ws As Worksheet
Dim pt As PivotTable
Application.ScreenUpdating = False
Set ws = ActiveSheet
lr = ws.UsedRange.Rows.Count
For Each pt In ws.PivotTables
If pt.Name <> "PivotTable1" Then 'Name of the Pivot Table which you don't want to hide
pt.TableRange2.EntireRow.Hidden = True
End If
Next pt
Application.ScreenUpdating = True
End Sub
If you want to show all the pivot tables again, you may try the below code...
Sub ShowAllPivotTables()
Dim ws As Worksheet
Dim pt As PivotTable
Application.ScreenUpdating = False
Set ws = ActiveSheet
lr = ws.UsedRange.Rows.Count
For Each pt In ws.PivotTables
pt.TableRange2.EntireRow.Hidden = False
Next pt
Application.ScreenUpdating = True
End Sub
I have a bunch of sheets with detailed data sets and pivot tables. On a summary sheet, I want to display just the pivot tables. (Of course, I'd rather stay DRY and not create a whole new set.) How can I reference the old pivot tables?
I can use VBA to do this if necessary.
This sub will keep the pivot tables 'live.' You could PasteValues over them if you don't want that.
Sub SummarizePivotTables()
Dim wb As Workbook, ws As Worksheet, ss As Worksheet, pt As PivotTable
Dim pasteRow As Long
Const rowsBetween As Long = 1
Set wb = ThisWorkbook
Set ss = wb.Worksheets("Summary")
pasteRow = 1 'first table row'
For Each ws In wb.Worksheets
For Each pt In ws.PivotTables
'change this to TableRange1 if you do not want the page field included'
With pt.TableRange2
.Copy ss.Range("A" & pasteRow)
pasteRow = pasteRow + .Rows.Count + rowsBetween
End With
Next pt
Next ws
End Sub
Pivot tables are named "Excel tables" in your workbook. So you should be able to do this without VB as I described in this answer.
tl;dr;
Data -> Get External Data -> Existing Connections
Goto "Tables" tab, select your table
Insert as "Table"