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.
- Click on the "Data Validation" button.
- Select List.
- Type Africa, Asia, Europe, North America, South America in Source:
- Click 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".
- Click the "Scatter" chart button.
- Click "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 executes 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.
- Right-click on the sheet name.
- Click on "View Code".
- Paste code (see below) to sheet module.
- Exit VB Editor
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 […]
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 […]
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 […]
Improve your X Y Scatter Chart with custom data labels
The picture above shows a chart that has custom data labels, they are linked to specific cell values. What's on […]
How to build an interactive map in Excel
This article describes how to create a map in Excel, the map is an x y scatter chart with an […]
This picture below shows you a column chart with pictures (flags) below each column. Watch this video to learn how […]
3 Responses to “Highlight group of values in an x y scatter chart programmatically”
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.
Thanks for the post! I keep getting an error message saying "Method 'Range' of object'_Worksheet' failed.
When opening VB Editor the following code is highlighted in yellow:
For r = 1 To Range("Table1[Country]").Rows.Count
Any idea what I may be missing?
I'm trying to make your exact chart as practice and then apply it to my data. In just making your chart I keep getting a bug error on this line "For r = 1 To Range("Table1[Country]").Rows.Count"
Is there a reason for this? I can't seem to make your exact chart.
Gretchen,
Table1[Country] is a reference to an Excel defined Table named Table1, [Country] is a reference to a column with a header named Country in Table1.
Make sure you adjust these references so they match the name of your Excel defined Table and header.