Change chart data range using a Drop Down List [VBA]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above image shows the drop-down list and the input range is cell range E2:E4.
Cell range E2:E4 contains the table names that points to Excel defined Tables located on sheet 2011, 2010 and 2009, see picture below. When you select a table name in the drop-down list, the chart is instantly populated and refreshed, see picture above.
Excel defined Tables are great to work with, they expand automatically when new values are added, this makes charts with data sources pointing to Excel defined Tables easy to work with. You don't need to adjust the cell references when records are added or deleted.
Here are the steps to set up this scenario:
Create a chart
- Go to "Insert" tab
- Click "Column chart" button
- Click "Clustered Column" chart button
Create Combo Box
- Go to "Developer" tab
- Click "Insert controls" button
- Click Combo Box (Form Control)
- Click and drag on the worksheet to create the combobox.
Add VBA code to a module
- Press Alt + F11 to open the VB Editor.
- Right click on your workbook in the Project Explorer window.
- Click "Insert"
- Click "Module" to insert a module to your workbook.
- Copy VBA code below (Ctrl + c)
- Paste VBA code to the code module, see image above.
- Return to Excel
VBA code
'Name macro Sub SelectTable() 'Apply actions to selected drop-down list With ActiveSheet.Shapes(Application.Caller).ControlFormat 'Make sure the selected drop-down list name is "Drop Down 1" If ActiveSheet.Shapes(Application.Caller).Name = "Drop Down 1" Then 'Change Chart 1 data source Worksheets("Chart").ChartObjects("Chart 1").Chart.SetSourceData Source:= _ Range(.List(.Value) & "[#All]") Worksheets("Chart").ChartObjects("Chart 1").Chart.PlotBy = xlRows End If End With End Sub
Assign macro
Populate Combo Box
How to use mouse hover on a worksheet [VBA]
I recently discovered an interesting technique about using a user defined function in a HYPERLINK function. Jordan at the Option […]
Create dependent drop down lists containing unique distinct values
This article explains how to build dependent drop down lists. Here is a list of order numbers and products. We […]
Populate drop down list with unique distinct values sorted from A to Z
Question: How do I create a drop-down list with unique distinct alphabetically sorted values? Table of contents Sort values using […]
Apply dependent combo box selections to a filter
Josh asks: now if i only knew how to apply these dependent dropdown selections to a filter, i'd be set. […]
Question: How do I create a chart that dynamically adds the values, as i type them on the worksheet? Answer: […]
How to use mouse hover on a worksheet [VBA]
I recently discovered an interesting technique about using a user defined function in a HYPERLINK function. Jordan at the Option […]
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
Open Excel files in a folder [VBA]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
Split data across multiple sheets [VBA]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
24 Responses to “Change chart data range using a Drop Down List [VBA]”
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.
How would I adapt this to work if I have a number of stacked series in the chart, each with a defined range? Each time I update the chart, I want it to pull data from the same sheet, same rows, just offset columns.
Amanda,
read this: Dynamic chart – Display values from a table row or column
How can I adapt the code to run on Excel 2003, I did in 2010 and works fine, but in 2003 had an error (Range error on method _Global)
Worksheets("Chart").ChartObjects("Chart 1").Chart.SetSourceData Source:= _Range(.List(.Value) & "[#All]")
Any clue?
Hernan Delgado,
Maybe this answers your question:
https://www.excelforum.com/excel-programming-vba-macros/386423-vba-error-unable-to-set-the-values-property-of-the-series-class.html
Hi,
When I insert the combobox at the end of the instructions, nothing is present in the drop down. I was wondering if you would be able to help with that?
Thanks for the help!
Ash
Ash,
I have added instructions, see above: Populate combo box
Amanda,
Thanks!
Hi Ash,
Select "Design Mode" on the Developer tab, then right click on the combobox and select Properties. In the ListFillRange field enter the range of cells that hold the values you would like in the dropdown. Close the Properties window, deselect "Design Mode" and I believe you should be good to go.
I have tabular data that is dymanic how could i change this vba to update a charts range from this tabular data. needs to be vba as the number of series changes when the data changes,
Charles,
please explain in greater detail or preferably upload a workbook without sensitive data.
[...] I'm probably going to go with the following method, when I get my data organised that is!!..... Change chart data range using a drop down list (vba) | Get Digital Help - Microsoft Excel resource Cheers. [...]
It works like charm. I was just wondering if there is a way to get the vba set the source data in a dual axis environment. Setting the source data on primary axis from the drop down, while leave the secondary axis intact, with its setting, or have another drop down for it?
I can't get it to work. Can you provide more details about how to link up the combo box to the actual tables please? When I try to run a macros, it also gives me "Type Mismatch" error message
Thanks,
Yuri
Yuri,
Check the combo box name and that it matches the shape name in the vba code.
Hi There,
I am getting the following error.
Run-Time error'-2147352571(80020005)
The item with the specified name wasn't found for the below line code
With ActiveSheet.Shapes(Application.Caller).ControlFormat
Manish,
Your drop down list must be named "Drop Down 1"
or change the name here accordingly:
If ActiveSheet.Shapes(Application.Caller).Name = "Drop Down 1" Then
Hi,
Found this thread and I am hoping it is still active.
Two Questions:
1. I have four items that need to be in my drop down box, instead of three. How would you go about adding a 2012 table to the spreadsheet and enable the macro to recognize the new sheet? When I tired I got an error message even though all I did was copy one of the existing sheets and renamed it.
2. Is there a way that I can rename the sheets using a word rather than a number? For instance, instead of the current name of the sheets (2009,2010,2011), I would like to use the words Gender, Race, etc. Again, I got an error message when I attempted to do this. In fact, I got the same error message in both cases.
Jeff Lucas,
1. I have four items that need to be in my drop down box, instead of three. How would you go about adding a 2012 table to the spreadsheet and enable the macro to recognize the new sheet? When I tired I got an error message even though all I did was copy one of the existing sheets and renamed it.
Add a new table and name the table, not the sheet, Table2012.
2. Is there a way that I can rename the sheets using a word rather than a number? For instance, instead of the current name of the sheets (2009,2010,2011), I would like to use the words Gender, Race, etc. Again, I got an error message when I attempted to do this. In fact, I got the same error message in both cases.
You can name the tables or sheets whatever you want, almost.
Download this example file, it has a fourth table named Table2012 and it is on sheet2.
change-chart-data-rangev2.xlsm
Thank you so much! This worked perfectly!
Hi Oscar, thanks for this amazing article - especially for updating with more basic how to create a drop-down menu it makes it so much more accessible.
I wanted to know, can this work where your tables are BETWEEN workbooks? Like, you have one Excel file for 2014, one for 2013, etc.
Many thanks.
Hi,
Can you tell me what if i want to change the data of chart and use this code to make the similar chart and can i also copy and paste it in my final presentation without showing the background work just chart with drop down
Hi,
I need exactly same thing. BUT instead of Tables, i have Pivot Tables, Which i want to switch as data Source for a Chart. Can you give the Macro for that?
Great Work Sir!
Gagan Brar,
A pivot chart or a regular chart?
tancks
Hi,
I have tried to populate a chart with values selected from drop down combo box list. My challenge here is it has populate from different column and row based on the value selected in the dropdown list. the cells will be constant like E and F column will only be selected for the chart but it might take from E1-23 or E 24-E30 or E30 - E40 depending on the value selected from combo box. combo box will have headers like for example server name, so based on the server name displayed the chart has to change by picking up values from E and F columns either E1-24 or E24-30 or E3-0-40,F1-24 or F24-30 or F3-0-40. I want this to be done automatically using VBA.
My Present Code:
Sub create_chart_sheet()
Dim oChartObj As ChartObject
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set oChartObj = ActiveSheet.ChartObjects.Add(Top:=0, Left:=0, Width:=50, Height:=50)
'Add Data to Chart Sheet
oChartObj.Chart.SetSourceData Sheets("log").Range("A2:F69")
Set oChartObj = ActiveSheet.ChartObjects.Add(Top:=60, Left:=400, Width:=450, Height:=450)
With oChartObj.Chart
'.ChartType = xlPie
.ChartType = xlBarClustered
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "Server Disk Usage"
.SeriesCollection(1).XValues = ActiveSheet.Range("F3:F23") ' Hardcoded for one server
.SeriesCollection(1).Values = ActiveSheet.Range("E3:E23")
Dim oChart As Chart
Set oChart = Sheets("log").ChartObjects(1).Chart
'Set oChartObj = Charts("log")
oChart.HasTitle = True
oChart.ChartTitle.Text = "Server Disk Usage"
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub