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
Built-in Charts
Combo Charts
Combined stacked area and a clustered column chartCombined chart – Column and Line on secondary axis
Combined Column and Line chart
Chart elements
Chart basics
How to create a dynamic chartRearrange data source in order to create a dynamic chart
Use slicers to quickly filter chart data
Four ways to resize a chart
How to align chart with cell grid
Group chart categories
Excel charts tips and tricks
Custom charts
How to build an arrow chartCustom chart examples
How to graph an equation
Build a comparison table/chart
Heat map yearly calendar
Advanced Gantt Chart Template
Sparklines
Win/Loss Column LineHighlight chart elements
Highlight a column in a stacked column chart no vbaHighlight a group of chart bars
Highlight a data series in a line chart
Highlight a data series in a chart
Highlight a bar in a chart
Interactive charts
How to filter chart dataHover with mouse cursor to change stock in a candlestick chart
How to build an interactive map in Excel
Highlight group of values in an x y scatter chart programmatically
Use drop down lists and named ranges to filter chart values
How to use mouse hover on a worksheet [VBA]
How to create an interactive Excel chart [VBA]
Change chart series by clicking on data [VBA]
Change chart data range using a Drop Down List [VBA]
How to create a dynamic chart
Animate
Line chart Excel Bar Chart Excel chartAdvanced charts
Custom data labels in a chartImprove your X Y Scatter Chart with custom data labels
Label line chart series
How to position month and year between chart tick marks
How to add horizontal line to chart
Add pictures to a chart axis
How to color chart bars based on their values
Excel chart problem: Hard to read series values
Build a stock chart with two series
Change chart axis range programmatically
Change column/bar color in charts
Hide specific columns programmatically
Dynamic stock chart
How to replace columns with pictures in a column chart
Color chart columns based on cell color
Heat map using pictures
Dynamic Gantt charts
Stock charts
Build a stock chart with two seriesDynamic stock chart
Change chart axis range programmatically
How to create a stock chart
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.