December 13, 2021
Hi,
I've googled for any way to automatically add a new column to a pivot table and insert sparklines inside it automatically but I was not able to do it automatically.
All I was able to do, was just to inset a sparkline in the cell far after the last column of the pivot table, then drag it till the end.
Is there any way to include it inside the pivot table in order to be updated automatically when data changes?
And if it is not possible, is it possible to insert a formula beside the pivot table that could be updated automatically with my interaction to drag the cell down to update the sparklines?
My point is to get an updated sparkline automatically when data inside the pivot table changed and add new rows automatically.
Thanks
The attached file includes a sheet titled "Sparklines-Inside-PivotTables" for the pivot table. K column includes the manually inserted sparklines, and as you can see I need to manually drag the cells down manually to update the sparklines.
My request please if there is any way to update the sparklines automatically shown in column M.
Or maybe there is any way to insert a table in the column M and let it get updated automatically according to the contents of the pivot table, then insert the sparklines inside the table cell, so when the pivot table gets updated the newly added tables get updated as well, and in turn, the sparklines get updated as well.
Thanks a lot and too much appreciated your valuable time reading my post and helping me.
July 16, 2010
Hi,
As you've found, it's not possible to include a Sparkline in a PivotTable. Please see this tutorial on relative dynamic named ranges. Towards the end there is an example that uses Sparklines.
I hope that points you in the right direction.
Mynda
Trusted Members
Moderators
November 1, 2018
You can also do it with code. Add this to a normal module:
Sub UpdatePivotSparklines(pt As PivotTable)
With pt
Dim sparkRange As Range
Set sparkRange = .DataBodyRange.Offset(, .DataBodyRange.Columns.Count).Resize(, 1)
End With
pt.TableRange1.Worksheet.UsedRange.SparklineGroups.Clear
With sparkRange.SparklineGroups
.Add Type:=xlSparkLine, SourceData:=pt.DataBodyRange.Address
With .Item(1)
With .SeriesColor
.Color = 9592887
.TintAndShade = 0
End With
With .Points
.Highpoint.Visible = True
.Lowpoint.Visible = True
.Firstpoint.Visible = True
.Lastpoint.Visible = True
.Markers.Visible = True
With .Negative.Color
.Color = 208
.TintAndShade = 0
End With
With .Markers.Color
.Color = 208
.TintAndShade = 0
End With
With .Highpoint.Color
.Color = 208
.TintAndShade = 0
End With
With .Lowpoint.Color
.Color = 208
.TintAndShade = 0
End With
With .Firstpoint.Color
.Color = 208
.TintAndShade = 0
End With
With .Lastpoint.Color
.Color = 208
.TintAndShade = 0
End With
End With
.Axes.Horizontal.RightToLeftPlotOrder = True
End With
End With
End Sub
then right click the pivot table worksheet, choose View Code and paste the following code into the code pane:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
UpdatePivotSparklines Target
End Sub
Macro enabled version of your file attached.
Answers Post
December 13, 2021
Hi,
Thanks a lot for your valuable time helping me.
Excellent indeed.
But, if I've several sheets (about 59 sheets), each one includes a pivot table, and each has sparklines beside it, of course.
Is it possible to set different colors and styles for each sparkling sheet? I do not want to let them all have the same look and theme, as with each pivot table refresh all sparklines reset to the same theme.
Thanks
MGB!
Trusted Members
Moderators
November 1, 2018
I do like the idea of a sparkling sheet! 😉
If you already have the sparklines in place, then the code can be a lot simpler:
Sub UpdatePivotSparklines(pt As PivotTable) With pt Dim sparkRange As Range Set sparkRange = .DataBodyRange.Offset(, .DataBodyRange.Columns.Count).Resize(, 1) End With With pt.TableRange1.Worksheet.UsedRange.SparklineGroups.Item(1) Set .Location = sparkRange .SourceData = pt.DataBodyRange.Address End With End Sub
December 13, 2021
Sub UpdatePivotSparklines(pt As PivotTable) With pt Dim sparkRange As Range Set sparkRange = .DataBodyRange.Offset(, .DataBodyRange.Columns.Count).Resize(, 1) End With With pt.TableRange1.Worksheet.UsedRange.SparklineGroups.Item(1) Set .Location = sparkRange .SourceData = pt.DataBodyRange.Address End With End Sub
Is this code to be added after the last line of the original code above?
Or it is to replace the old code with the new one?
If you already have the sparklines in place, then the code can be a lot simpler:
Please note that I can not determine the future growth of the pivot table growth, as many rows are to be added later on.
I've tried to paste the above code after the end of the original code but got an error.
Thanks a lot and much appreciated your valuable time helping me my friend.
MGB
Trusted Members
Moderators
November 1, 2018
December 13, 2021
Thanks a lot for your valuable time replying to my thread and helping me.
But if I want to merge both VBAs into one single code, what would it be?
The reason for asking is that I need to add the sparklines to other workbooks and I want to randomize its theme.
So I want to keep the full code to achieve the 2 things in the same newly created workbook.
Thanks a lot.
Trusted Members
Moderators
November 1, 2018
December 13, 2021
Please, I mean if I am creating a new workbook, and this new workbook has several sheets.
Each sheet includes a pivot table and no sparklines had been added yet at all.
And I want the format or the theme of the newly added sparklines for each sheet beside its associated pivot table to be a random theme.
So, I am asking what would be the code to do both tasks for the new workbook that has a pivot table only.
1st task: Add sparklines.
2nd task: Set the sparklines theme to be a random theme.
Sorry if my poor English does not help me to express myself correctly.
Thanks a lot for your help.
Trusted Members
Moderators
November 1, 2018
Unfortunately there is no option in VBA to simply apply a theme to a sparkline. You have to individually set the colours of the line, marker, high point, low point, first point and last point for example. Doing that randomly will most likely produce terrible combinations a lot of the time. I would personally suggest that you pick a theme and stick to it.
Trusted Members
Moderators
November 1, 2018
If you really wanted to pursue it, one option might be to create arrays with related colour values in (eg one has colour values for the line, one for the markers and so on) then use something like Randbetween to select a particular item from each array. At least that way you can design some combinations that work well beforehand.
1 Guest(s)