Forum

Notifications
Clear all

Add sparklines automatically to a pivot table column

14 Posts
3 Users
0 Reactions
943 Views
(@mgbsher)
Posts: 35
Trusted Member
Topic starter
 

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.

 
Posted : 28/11/2022 7:59 am
(@mynda)
Posts: 4761
Member Admin
 

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

 
Posted : 29/11/2022 9:52 pm
(@mgbsher)
Posts: 35
Trusted Member
Topic starter
 

Thanks a lot Mynda ... I will follow your advice.

 
Posted : 30/11/2022 2:29 am
(@debaser)
Posts: 837
Member Moderator
 

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.

 
Posted : 30/11/2022 5:12 am
(@mgbsher)
Posts: 35
Trusted Member
Topic starter
 

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!

 
Posted : 08/12/2022 3:04 pm
(@debaser)
Posts: 837
Member Moderator
 

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
 
Posted : 09/12/2022 7:31 am
(@mgbsher)
Posts: 35
Trusted Member
Topic starter
 
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

 
Posted : 10/12/2022 1:29 am
(@debaser)
Posts: 837
Member Moderator
 

That routine replaces the previous one of the same name. The worksheet code that calls it remains the same.

 

as long as there are spark lines on the pivot sheets it should work.

 
Posted : 11/12/2022 4:01 am
(@mgbsher)
Posts: 35
Trusted Member
Topic starter
 

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.

 
Posted : 11/12/2022 4:18 am
(@debaser)
Posts: 837
Member Moderator
 

I don’t really understand. One code adds and formats spark lines while the other only adjusts the location and data range of existing spark lines. It doesn’t make sense to combine them.

 
Posted : 12/12/2022 2:32 pm
(@mgbsher)
Posts: 35
Trusted Member
Topic starter
 
Thanks a lot for your valuable time helping me.

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.

 
Posted : 13/12/2022 12:31 pm
(@debaser)
Posts: 837
Member Moderator
 

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.

 
Posted : 15/12/2022 9:01 am
(@mgbsher)
Posts: 35
Trusted Member
Topic starter
 

Thanks a lot.

 
Posted : 15/12/2022 1:40 pm
(@debaser)
Posts: 837
Member Moderator
 

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.

 
Posted : 16/12/2022 6:15 am
Share: