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
- Press with right mouse button on combo box.
- Press with left mouse button on "Format control..." and a dialog box appears, see image below.
- Go to tab "Control".
- Press with left mouse button on "Input range" button.
- Select cell range E2:E4.
- Press with left mouse button on OK button.
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
How to add lines between stacked columns/bars [Excel charts]
Custom charts
How to build an arrow chartHow to graph a Normal Distribution
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 chartHighlight a group of chart bars
Highlight a data series in a line chart
Highlight a column in a stacked column 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
One Response to “How to create a stock chart”
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.
[…] Learn how to create a stock chart in excel […]