How to animate a line chart
This article demonstrates how to create an animation using a line chart in Excel. The user selects a series in a drop-down box and a macro animates the chart.
What's on this page
The animated image below shows what happens when the user selects a series, as you can see the chart has a small trailing shadow effect.
How I made this chart
The chart data is in cell range B3:J8. A small VBA macro calculates the difference between the current series and the new series and then divides it by 20. The result is shown in cell range C10:J10.
The chart data source is cell range C13:J17. The chart uses 4 lines to create the trailing shadow effect. Series 1 is the main line, the others are shadows.
Here you can see the calculations in action.
Event code
'Event code that fires as soon as any cell value in the worksheet has changed Private Sub Worksheet_Change(ByVal Target As Range) 'If ... then statement - Check if target cell adress is equal to L6 If Target.Address = "$L$6" Then 'Enable error handling On Error Resume Next 'Find position of value saved cell L6 in cell range B4:B8 and save to variable x x = WorksheetFunction.Match(Range("L6"), Range("B4:B8"), 0) - 1 'Stop macro if an error has occurred If Err > 0 Then Exit Sub 'Disable error handling On Error GoTo 0 'For ... Next statement- Iterate from 1 to 8 using variable i For i = 1 To 8 'Calculate and save value from data table to cell range C13:J13 Range("C10:J10").Cells(i) = (Range(Cells(4 + x, "C"), Cells(4 + x, "J")).Cells(i) - _ Range("C13:J13").Cells(i)) / 20 Next i 'For ... Next statement- Iterate from 1 to 20 using variable i For i = 1 To 20 'Save values in cell range C13:J16 to cell range C14:J17 Range("C14:J17") = Range("C13:J16").Value 'For ... Next statement- Iterate from 1 to 8 using variable j For j = 1 To 8 'Add cell value from cell range C13:J13 and C10:J10 and save to cell range C13:J13 based on variable j Range("C13:J13").Cells(j) = Range("C13:J13").Cells(j) + Range("C10:J10").Cells(j) Next j 'Show changes on screen DoEvents 'And once again to make it work in Excel 365 DoEvents Next i For j = 1 To 4 Range("C14:J17") = Range("C13:J16").Value Range("C14:J14") = "" DoEvents Next j End If End Sub
Where to put the code?
- Righ-press with left mouse button on the worksheet tab located on the bottom left corner, see image above. A pop-up menu appears.
- Press with mouse on "View Code".
- The Visual Basic Editor opens up with the corresponding worksheet module open.
- Copy VBA code above.
- Paste VBA code to the worksheet module.
- Return to Excel.
Recommended articles
Animate category
This article demonstrates macros that animate bars in a chart bar. The image above shows a bar chart that animates […]
This article demonstrates how to create a chart that animates the columns when filtering chart data. The columns change incrementally […]
Charts category
Question: How do I create a chart that dynamically adds the values, as i type them on the worksheet? Answer: […]
I recently discovered an interesting technique about using a user defined function in a HYPERLINK function. Jordan at the Option […]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
Line chart category
This article demonstrates how to highlight a line in a chart based on the selected item in a drop-down list. […]
The chart above contains no legend instead data labels are used to show what each line represents. Table of Contents […]
The line chart lets you chart data points as a line, this chart type is useful if you have many […]
Excel categories
2 Responses to “How to animate a line chart”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Hi Great Work and Superb Coding,
I have 1 observation, why always the results of the dropdown is reflected only on Range("C13:J13")
I believe ideally the results against the select dropdown criteria
Like if i select series4, the results should appear parallel to series4.
Also i suggest there should be an Overall dropdown series to cover all the analysis in the selection
Best regards,
Sajjad
This is a new inspiration for many people. Thank you for sharing with us.