Sparklines with Rolling 12 Months

Zee996

New Member
Joined
Nov 30, 2021
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hi, I am building a dashboard. So tab 1 called "Data" with cell T2 has a list with a dropdown with the month of the year. I am looking for a way for sparkling to show the data of rolling 12 months corresponds to the data selected in the drop-down.

For example, if we select Feb 22, the sparkline should capture Feb 22 to Feb 23, if we choose Mar 23, the Sparkline should capture Mar 22 to Mar 23, etc.

I have the formula that captures the 12 months of data with the help of name range I can create dynamic sparklines.
Formula =OFFSET(Sheet1!$C$1,COUNT(Sheet1!$C:$C),0,-12,1)

But I am stuck on how to inbuild these drop-downs in the formula.

Is this possible to create?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
First

Here is a flowchart that illustrates the logic of the VBA code for creating a dynamic sparkline based on a selected month:
VBA Code:
START

Define selectedMonth as range T2
Define dataRange as range C2:C100

Find the last row of data in dataRange
Calculate start and end dates for sparkline based on selectedMonth
Create sparkline range using OFFSET function and SPARKLINE function
Update formula of sparkline range to include data range and start/end dates

If selectedMonth is changed, then call UpdateSparkline subroutine

END

now I rather to create a dynamic sparkline using VBA, you can use the following code:
VBA Code:
Sub UpdateSparkline()
    Dim selectedMonth As Range
    Dim dataRange As Range
    Dim sparklineRange As Range
    
    Set selectedMonth = Range("T2")
    Set dataRange = Range("C2:C100") 'Assuming your data is in column C
    
    'Find the last row of data
    Dim lastRow As Long
    lastRow = dataRange.Cells(dataRange.Cells.Count).End(xlUp).Row
    
    'Calculate the start and end dates for the sparkline
    Dim startDate As Date
    Dim endDate As Date
    endDate = selectedMonth.Value
    startDate = DateAdd("m", -11, endDate)
    
    'Create the sparkline range using the OFFSET function
    Set sparklineRange = Range("D2") 'Assuming your sparkline starts in column D
    sparklineRange.Formula = "=SPARKLINE(OFFSET(" & dataRange.Address & ", MATCH(" & startDate & "," & dataRange.Address & ",0)-1, 0, 12, 1))"
End Sub
This code first defines the selected month and data range, and then calculates the start and end dates for the sparkline based on the selected month. It then creates the sparkline range using the OFFSET function and the SPARKLINE function. Finally, it updates the formula of the sparkline range to include the correct data range and start/end dates.

You can then call this code from the worksheet using the Worksheet_Change event, like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("T2")) Is Nothing Then
        UpdateSparkline
    End If
End Sub
This code checks if the value of cell T2 has changed, and if so, calls the UpdateSparkline subroutine. This ensures that the sparkline is updated automatically whenever the user selects a different month from the dropdown list.
 
Upvote 0
Solution
I hope you find help full and see how important is to explained the logic before the code.
 
Upvote 0
Thank you so much for this. It is really helpful and thank you for explaining the logic behind the code.

Thank you so much again :)
 
Upvote 0
i tried to build it with a function built into the sparklines dialog, but I could not figure it out:
but try this:
Book1
ABCDEF
1DateValueChoose Date:
22022-01-0112022-06-01
32022-02-011
42022-03-011Sparklines Data
52022-04-0112
62022-05-0122
72022-06-0122
82022-07-0123
92022-08-0123
102022-09-0133
112022-10-0133
122022-11-0134
132022-12-0134
142023-01-0144
152023-02-0144
162023-03-0145
172023-04-014
182023-05-015
192023-06-015
202023-07-015
212023-08-015
222023-09-016
232023-10-016
242023-11-016
252023-12-016
262024-01-017
Zee996
Cell Formulas
RangeFormula
D2D2=DATE(2022,6,1)
F5:F16F5=INDEX($B$2:$B$26,MATCH($D$2,$A$2:$A$26,0),1):INDEX($B$2:$B$26,MATCH(EDATE($D$2,11),$A$2:$A$26,0),1)
Dynamic array formulas.





1679510482003.png
 
Upvote 0
i tried to build it with a function built into the sparklines dialog, but I could not figure it out:
but try this:
Book1
ABCDEF
1DateValueChoose Date:
22022-01-0112022-06-01
32022-02-011
42022-03-011Sparklines Data
52022-04-0112
62022-05-0122
72022-06-0122
82022-07-0123
92022-08-0123
102022-09-0133
112022-10-0133
122022-11-0134
132022-12-0134
142023-01-0144
152023-02-0144
162023-03-0145
172023-04-014
182023-05-015
192023-06-015
202023-07-015
212023-08-015
222023-09-016
232023-10-016
242023-11-016
252023-12-016
262024-01-017
Zee996
Cell Formulas
RangeFormula
D2D2=DATE(2022,6,1)
F5:F16F5=INDEX($B$2:$B$26,MATCH($D$2,$A$2:$A$26,0),1):INDEX($B$2:$B$26,MATCH(EDATE($D$2,11),$A$2:$A$26,0),1)
Dynamic array formulas.
shoot, the sparklines did not appear.. I'll post an image:
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,859
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top