Interactive chart in excel (vba)
I found a basic interactive chart on the chitika website and my first thought was if I could do this in excel. So now I would like to share this simple chart with three buttons.
You can select multiple buttons and the chart is updated instantaneously. You can also deselect buttons.
How to create the chart
Create a table
- Select data cell range
- Go to tab "Insert"
- Click "Table" button
- Click OK
Create chart
- Select your table
- Go to tab "Insert"
- Click "Columns" button
- Click "2D Clustered column"
- Right click on chart
- Select "Select data"
- Click "Switch row/column" button
- Click OK
Insert shapes
- Go to tab "Insert"
- Click "Shapes" button
- Click "Rounded Rectangle"
- Create three rectangles beneath the chart
- Right click on rectangle and select "Edit text"
- Type button names and make sure they match the table data in the first column (Revenue, Earnings and Employees)
VBA
Sub Chart()
Dim temp As Variant
Dim Series() As String
Dim i As Single
ReDim Series(0)
Application.ScreenUpdating = False
With ActiveSheet.Shapes(Application.Caller).Fill.ForeColor
If .Brightness = 0 Then
.Brightness = -0.150000006
Else
.Brightness = 0
End If
End With
temp = Array("Rounded Rectangle 1", "Rounded Rectangle 2", "Rounded Rectangle 3")
For i = LBound(temp) To UBound(temp)
With ActiveSheet.Shapes(temp(i))
If .Fill.ForeColor.Brightness = -0.150000006 Then
Series(UBound(Series)) = .TextFrame2.TextRange.Characters.Text
ReDim Preserve Series(UBound(Series) + 1)
End If
End With
Next i
If UBound(Series) > 0 Then ReDim Preserve Series(UBound(Series) - 1)
Worksheets("Sheet1").ListObjects("Table1").Range.AutoFilter Field:=1
Worksheets("Sheet1").ListObjects("Table1").Range.AutoFilter _
Field:=1, Criteria1:=Series, Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub
- Copy vba code
- Open vb editor (Alt + F11)
- Select your workbook in the project explorer
- Click "Insert" on the menu
- Click "Module"
- Paste to code window
Assign macro
- Right click on the first rectangle
- Select "Assign macro"
- Select "chart" macro
- Click OK
Repeat above steps with the remaining rectangles.
Recommended posts:
Interactive Sales Chart using MS Excel [video]
Download excel *.xlsm file
Related posts:
Learn how to create a stock chart in excel
Adjust stock chart axis automatically
Change chart data range using a drop down list (vba)
Use a mouse hovering technique to create an interactive chart



















Hi Oscar, excellent site and very helpful and easy to follow solutions and tips. I've been a long time vistor to this site and you have solved many problems for me, thanks.
I am trying to use the above code, but I get this error... -2147352571 (80020005) Type mismatch
On this line...
Any suggestions on what may be causing this error?
My "Table" is in the range..U4:X7
My Chart is on a seperate sheet (Dashboard) to the Table
Thanks
Ak
Ak,
I am using rounded rectangles as buttons.
Insert | Shapes | Rectangles
If you click a shape the following code changes it´s color:
With ActiveSheet.Shapes(Application.Caller).Fill.ForeColor If .Brightness = 0 Then .Brightness = -0.150000006 Else .Brightness = 0 End If End WithMaybe you are not using shapes?
Hi Oscar, thanks for the reply.
I am indeed using shapes, as directed in your instructions above, Rounded Rectangles.
I have just opened the workbook and tried again and I am now getting this error...
Run-time error '438':
Object doesn't support this property or method
on this line...
MS Help offers me this...
"Object doesn't support this property or method (Error 438)
Not all objects support all properties and methods. This error has the following cause and solution:
You specified a method or property that doesn't exist for this Automation object.
See the object's documentation for more information on the object and check the spellings of properties and methods.
You specified a Friend procedure to be called late bound.
The name of a Friend procedure must be known at compile time. It can't appear in a late-bound call."
But that doesn't tell me anything!!
Sorry to be an idiot about this.
Ak
Ak,
Can you upload an example file?
The rounded rectangles were named Rounded Rectangle 7, 10 and 11 on AK´s sheet.
Change this line:
temp = Array("Rounded Rectangle 1", "Rounded Rectangle 2", "Rounded Rectangle 3")
to
temp = Array("Rounded Rectangle 7", "Rounded Rectangle 10", "Rounded Rectangle 11")
I'm getting the following error:
Run-time error ‘-2147024809 (80070057)’
The item with specified name wasn’t found.
With ActiveSheet.Shapes(temp(i))
What could be wrong? How do I find the name of my rounded rectangle shapes? Maybe that's the problem...
Thank you!
Rodrigo Canar,
How do I find the name of my rounded rectangle shapes?
1. Select a shape (Right click on the shape)
2. Read the name in the name box
Thank you. That was easy and the macro now is running without giving me any error messages, but it is not working properly. Every time I click over one of the three buttons, the displayed graph is not accurate. If I repeat a click in the same button, the result changes every time... Any idea?