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
- Press with left mouse button on "Column chart" button
- Press with left mouse button on "Clustered Column" chart button
Create Combo Box
- Go to "Developer" tab
- Press with left mouse button on "Insert controls" button
- Press with left mouse button on Combo Box (Form Control)
- Press with left mouse button on and drag on the worksheet to create the combobox.
Add VBA code to a module
- Press Alt + F11 to open the VB Editor.
- Press with right mouse button on on your workbook in the Project Explorer window.
- Press with left mouse button on "Insert"
- Press with left mouse button on "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
- Press with right mouse button on combo box.
- Press with left mouse button on "Assign macro...".
- Press with left mouse button on "SelectTable".
- Press with left mouse button on OK button.
Populate Combo Box
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 […]
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. […]
Drop down lists category
This article explains how to build dependent drop down lists. Here is a list of order numbers and products. We […]
Question: How do I create a drop-down list with unique distinct alphabetically sorted values? Table of contents Sort values using […]
Josh asks: now if i only knew how to apply these dependent dropdown selections to a filter, i'd be set. […]
This article demonstrates different ways to reference an Excel defined Table in a drop-down list and Conditional Formatting. There are […]
This article demonstrates how to set up dependent drop-down lists in multiple cells. The drop-down lists are populated based on […]
This article demonstrates how to run a VBA macro using a Drop Down list. The Drop Down list contains two […]
In this article, I am going to show you how to quickly change Pivot Table data source using a drop-down […]
This article describes how to create a drop-down list populated with sorted values from A to Z. The sorted list […]
A drop-down list in Excel prevents a user from entering an invalid value in a cell. Entering a value that […]
Sharmila asks: How can i use these list for multiple rows? I would like to use these lists for multiple […]
This article demonstrates a basic invoice template I created. It lets you use dropdown lists to quickly select products on […]
I will in this article demonstrate how to use a value from a drop-down list and use it to do […]
I will in this article demonstrate how to set up two drop down lists linked to an Excel chart, the […]
Aynsley Wall asks: I have a spreadsheet that I use for 3 different companies. What I would really like to […]
This article describes how to create a map in Excel, the map is an x y scatter chart with an […]
Josh asks: I have this working right now with 6 drop downs/lists. I wanted to see if you possibly know […]
This article demonstrates how to populate a drop down list with filtered values from an Excel defined Table. The animated […]
Question: Is there a way to have a unique list generated from a list? Meaning I have a sheet that […]
The drop down calendar in the image above uses a "calculation" sheet and a named range. You can copy the drop-down […]
Interactive 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 […]
This article describes how to create an interactive chart, the user may press with left mouse button on a button […]
The image above shows a chart populated with data from an Excel defined Table. The worksheet contains event code that […]
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 use drop down lists combined with an Excel defined Table and a chart. This allows […]
This article demonstrates how to change chart series while hovering with mouse cursor over a series name. The image above […]
I will in this article demonstrate how to highlight a group of values plotted in an x y scatter chart […]
What if you want to show a selection of a data set on a chart and easily change that selection? […]
Macro category
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]
This article demonstrates how to run a VBA macro using a Drop Down list. The Drop Down list contains two […]
This workbook contains two worksheets, one worksheet shows a calendar and the other worksheet is used to store events. The […]
What's on this page Copy a file Copy and rename a file Rename a file List files in a folder […]
Excel does not resize columns as you type by default as the image above demonstrates. You can easily resize all […]
This article describes how to create an interactive chart, the user may press with left mouse button on a button […]
Rahul asks: I want to know how to create a vlookup sheet, and when we enter a name in a […]
This article describes how to create a button and place it on an Excel worksheet then assign a macro to […]
Question: hi all, thanks for the great formula/array formula. it works great. lately, i noticed that the array formula will […]
This article demonstrates a macro that inserts new worksheets based on names in a cell range. The cell range may […]
In this article, I am going to demonstrate a simple workbook where you can create or delete projects and add […]
Today I would like to share with you these small event handler procedures that make it easier for you to […]
This article explains how to hide a specific image in Excel using a shape as a button. If the user […]
This article describes different ways to locate literal or hardcoded values in formulas. The image above shows the result from […]
This article demonstrates macros that save worksheets to a single pdf file. What's on this webpage Export all worksheets in […]
Excel categories
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 press with right mouse button 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.
Get the following Excel 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