How to animate an Excel Bar Chart
This article demonstrates macros that animate bars in a chart bar. The image above shows a bar chart that animates one bar at a time, the bar grows until it reaches the correct value.
The "Clear chart" button clears all values in the chart. The "Animate" button starts the animation. Check out my other animated chart:An animated Excel chart
What's on this page
Slower animation (Previous Excel versions)
The animated image above shows a bar chart animation that has bars that grow more slowly. Press with left mouse button on the "Clear chart" button located to the right of the chart to delete the chart bars.
Press with left mouse button on the "Animate" button located below the "Clear chart" button to start the animation. The "Animate" button has a macro assigned named ClearChart displayed below.
VBA Code
'Name macro Sub ClearChart() 'Delete values in cell range F3:F13 Range("F3:F13") = "" End Sub
'Name macro Sub Animate() 'Dimension variables and declare data types Dim i As Single, j As Integer, k As Single 'For - next statement, count backwards from 11 to 1 using variable j For j = 11 To 1 Step -1 'Save value from cell range C3:C13 to variable k based on variable j k = Range("C3:C13").Cells(j).Value 'For - next statement, count from 40 to number stored in variable k using variable i For i = 40 To k 'Save value in variable i to cell in cell range F3:F13 based on number stored in variable j Range("F3:F13").Cells(j) = i 'show changes on screen DoEvents Next i Next j End Sub
Where to put the VBA code?
You must copy the VBA code and paste it in a module in your workbook before you can use the macros.
- Copy the VBA code above or below on this webpage.
- Press Alt + F11 to open the Visual Basic Editor.
- Press with mouse on the "Insert" on the top menu, see image above.
- Press with mouse on "Module" to create a module. A module appears in the "Project Explorer" window. This is also shown in the image above.
- Paste the VBA code to the code module.
- Go back to Excel.
Explaining the chart animation
The bar chart's data source is cell range E2:F13. The macro begins with cell F13 and starts with value 40.
Then it adds 1 to 40, up to the value in C13 (45). The next cell is F12, starts at 40 and goes up to 50, one by one, and so on. See the picture below. The chart shows these changes in the data source as an animation.
Slower animation (Excel 365 subscription)
'Name macro Sub ClearChart() 'Delete values in cell range F3:F13 Range("F3:F13") = "" End Sub
'Name macro Sub Animate() 'Dimension variables and declare data types Dim i As Single, j As Integer, k As Single 'For - next statement, count backwards from 11 to 1 using variable j For j = 11 To 1 Step -1 'Save value from cell range C3:C13 to variable k based on variable j k = Range("C3:C13").Cells(j).Value 'For - next statement, count from 40 to number stored in variable k using variable i For i = 40 To k 'Save value in variable i to cell in cell range F3:F13 based on number stored in variable j Range("F3:F13").Cells(j) = i 'show changes on screen DoEvents 'Repeat to make it work in Excel 365 Next i Next j End Sub
A faster animation (Previous Excel versions)
The animated image above shows a much faster bar chart animation.
VBA Code
Sub ClearChart() Range("F3:F13") = "" End Sub
'Name macro Sub Animatev2() 'Dimension variables and declare data types Dim i As Single, j As Integer, k As Single, s As Single 'For ... Next statement. Go from 11 to 1 using variable j For j = 11 To 1 Step -1 'Save value from cell in cell range C3:C13 to variable k based on variable j k = Range("C3:C13").Cells(j).Value 'This line makes the animation faster, it calculates how fast the jump between numbers will be s = (k - 40) / 10 'For ... Next statement. Go from 40 to number saved in variable k using variable i and s For i = 40 To k Step s 'Save rounded number saved in variable i to cell in cell range F3:F13 based on variable j Range("F3:F13").Cells(j) = Round(i, 0) 'Show changes on screen DoEvents Next i Next j End Sub
Faster animation (Excel 365 subscription)
Sub ClearChart() Range("F3:F13") = "" End Sub
'Name macro Sub Animatev2() 'Dimension variables and declare data types Dim i As Single, j As Integer, k As Single, s As Single 'For ... Next statement. Go from 11 to 1 using variable j For j = 11 To 1 Step -1 'Save value from cell in cell range C3:C13 to variable k based on variable j k = Range("C3:C13").Cells(j).Value 'This line makes the animation faster, it calculates how fast the jump between numbers will be s = (k - 40) / 10 'For ... Next statement. Go from 40 to number saved in variable k using variable i and s For i = 40 To k Step s 'Save rounded number saved in variable i to cell in cell range F3:F13 based on variable j Range("F3:F13").Cells(j) = Round(i, 0) 'Show changes on screen DoEvents 'Repeat DoEvents to make it work in Excel 365 DoEvents Next i Next j End Sub
Chart Bars (Previous Excel versions)
'Name macro Sub Animatev3() 'Dimension variables and declare data types Dim i As Single, j As Integer, k As Single, s As Single For ... Next statement, iterate from 11 to 1 For j = 11 To 1 Step -1 'Save value from cell range C3:C13 based on variable j k = Range("C3:C13").Cells(j).Value 'Create a small delay using a FOR .. NEXT statement For i = 1 To 1000 'Refresh screen DoEvents Next i 'Save variable k to cell in cell range F3:F13 based on variable j Range("F3:F13").Cells(j) = k Next j End Sub
Chart Bars (Excel 365 subscription)
'Name macro Sub Animatev3() 'Dimension variables and declare data types Dim i As Single, j As Integer, k As Single, s As Single For ... Next statement, iterate from 11 to 1 For j = 11 To 1 Step -1 'Save value from cell range C3:C13 based on variable j k = Range("C3:C13").Cells(j).Value 'Create a small delay using a FOR .. NEXT statement For i = 1 To 1000 'Refresh screen DoEvents 'A second DoEvents is needed to make it work in Excel 365 DoEvents Next i 'Save variable k to cell in cell range F3:F13 based on variable j Range("F3:F13").Cells(j) = k Next j End Sub
Animate category
This article demonstrates how to create an animation using a line chart in Excel. The user selects a series in […]
This article demonstrates how to create a chart that animates the columns when filtering chart data. The columns change incrementally […]
Bar chart category
The bar chart is simply a column chart rotated 90 degrees right, this makes it great if you have long […]
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 […]
This article demonstrates macros that automatically changes the chart bar colors based on the corresponding cell, the first example is […]
This article describes how to create an interactive chart, the user may press with left mouse button on a button […]
The calendar shown in the image above highlights events based on frequency. It is made only with a few conditional […]
Fatou asks: Going back to my question, I had created a table and used the data to create a chart. […]
You can easily change data labels in a chart. Select a single data label and enter a reference to a […]
The image above shows a chart populated with data from an Excel defined Table. The worksheet contains event code that […]
Today I am going to show you how to create a dynamic Gantt chart in excel 2007. A Gantt chart helps […]
The picture above shows a chart that has custom data labels, they are linked to specific cell values. This means […]
I will in this article demonstrate how to set up two drop down lists linked to an Excel chart, the […]
This article describes how to create a map in Excel, the map is an x y scatter chart with an […]
This article demonstrates how to insert pictures to a chart axis, the picture above shows a column chart with country […]
I made a heat map calendar a few months ago and it inspired me to write this article. The heat […]
This article demonstrates a macro that changes y-axis range programmatically, this can be useful if you are working with stock […]
This article demonstrates how to highlight a bar in a chart, it allows you to quickly bring attention to a […]
This article describes a macro that hides specific columns automatically based on values in two given cells. I am also […]
This article demonstrates how to use drop down lists combined with an Excel defined Table and a chart. This allows […]
This article demonstrates how to highlight a line in a chart based on the selected item in a drop-down list. […]
Excel categories
4 Responses to “How to animate an Excel Bar 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 Oscar,
very nice animated bar
what is the "DoEvents" do ?
thank a lot
liam
[…] An animated bar chart […]
Hi Oscar,
When I try to use the code it gives me syntax error on For I = 0 to k
Sub Animate()
Dim i As Single, j As Integer, k As Single, s As Single
For j = 23 To 1 Step -1
k = Range("C3:C25").Cells(j).Value
For i = 0 To k
Range("F3:F25").Cells(j) = Round(i, 0)
DoEvents
Next i
Next j
End Sub
End Sub
My data
GOURAV NAGPAL 0
ZHONGPING MA 250
SUNIL DHANTOLE 200
ROOPA JACOB 550
GLAVIN GEORGE DSOUZA 0
MOHD AZAM SIDDIQUI 0
ALPHONES ALXEANDER 0
NITIN AVINASH DESHPANDE 0
YINGZI ZHANG 0
SANTUNU BARUA 0
ASHITA DHAKAN 0
XI WEI 0
J KRISHNA KISHORE RAO 0
KEVIN GEORGE 0
ABIJITH ASHOK MENON 0
SHUBHANGI PALIWAL 0
RAJEED VARGHESE KURIANPARAMBIL PAULOSE 0
CLIFFORD CHRISTOPHER 0
SHIVAM DUBEY 0
SATHISH KUMAR RAJARAM 300
HEMANT ARJUNSINGH THAKUR 0
RAWAD ALSET 0
ROHINI BHATT 0
Smitesh
Contains cell range C3:C25 numbers?