Highlight group of values in an x y scatter chart programmatically
I will in this article demonstrate how to highlight a group of values plotted in an x y scatter chart using a Drop Down List and a small VBA macro.
The example used here lets you select a region and the chart instantly highlights the corresponding countries, it also shows the names of those countries next to the values.
The animated image below shows how the chart changes when a new region is selected.
Add a Drop Down List
A regular Drop Down list in Excel is easy to create, it allows you to control the value in a given cell using Excel's Data Validation. Excel returns an error message if the value is not in the Drop Down List.
The most significant disadvantage is they are easy to remove, copy a cell and paste it to the cell containing the Drop Down List and it is gone, you need VBA code to protect it from being overwritten if this is an issue for you.
A smaller disadvantage is that you can't see if a cell contains a Drop Down List or not until you select the actual cell. I recommend that you change the cell color or create a cell border, so the user can quickly identify the Drop Down List. Here are the steps to create a Drop Down List:
- Select cell I18.
- Go to tab "Data" on the ribbon.
- Press with mouse on the "Data Validation" button.
- Select List.
- Type Africa, Asia, Europe, North America, South America in Source:
- Press with left mouse button on OK to create a Drop Down List
Insert a scatter chart
A scatter chart shows the relationships between two sets of values. My graph displays the relationship between GDP per Capita and life expectancy for a few countries.
The Drop Down List allows the user to select a group of Countries to highlight data in the chosen region, making the chart more comfortable to read.
It makes the worksheet more fun to use as it is now interactive allowing the user to examine the data in a way they prefer.
- Select the numbers in table table1
- Go to tab "Insert".
- Press with left mouse button on the "Scatter" chart button.
- Press with left mouse button on "Scatter with only markers".
Change y and x-axis formatting.
VBA code
Microsoft Excel lets you create programs called macros that you can use in your workbook, macros, and User-defined Functions consists of VBA code. VBA stands for Visual Basic for Applications, and a macro is easy to add to a workbook, there are detailed instructions below.
This particular macro is an event macro that runs when a cell value changes in a worksheet. In this case, it looks for a change in cell I18. Event macros are a bit different than regular macros. They are located in a worksheet module and not in a regular module.
I will explain how it works and how to implement it.
'Event macro Private Sub Worksheet_Change(ByVal Target As Range) 'Dimension variable and declare data type Dim r As Integer 'Check if cell is I18 If Target.Address = "$I$18" Then 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. With ActiveSheet.ChartObjects(1).Chart 'The FOR ... NEXT statement iterates from 1 to the number of rows in column Country in Table1 using variable r as a container For r = 1 To Range("Table1[Country]").Rows.Count 'Check if value in column Region in Excel table Table1 based on variable r is equal to the value in cell I18 If Range("Table1[Region]").Cells(r).Value = Range("$I$18") Then 'Change color of chart marker based on variable r .SeriesCollection(1).Points(r).Format.Fill.ForeColor.RGB = RGB(0, 0, 255) 'Show Data Labels .SeriesCollection(1).Points(r).ApplyDataLabels 'Use country name as Data Label text .SeriesCollection(1).Points(r).DataLabel.Text = Range("Table1[Country]").Cells(r).Value Else 'Change marker color .SeriesCollection(1).Points(r).Format.Fill.ForeColor.RGB = RGB(198, 198, 198) 'Enable errror handling On Error Resume Next 'Delete Data Label .SeriesCollection(1).Points(r).DataLabel.Delete 'Disable error handling On Error GoTo 0 End If Next r End With End If End Sub
Where to put the VBA code?
Follow these instructions to access the sheet module for a specific worksheet.
- Press with right mouse button on on the sheet name.
- Press with mouse on "View Code".
- Paste code (see below) to sheet module.
- Exit VB Editor
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 […]