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
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 […]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
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 will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
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 […]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
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 […]
What if you want to show a selection of a data set on a chart and easily change that selection? […]
Scatter x y chart category
The picture above shows a chart that has custom data labels, they are linked to specific cell values. This means […]
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 […]
The picture above shows the following equation x^3+3*x^2-3 plotted on an x y scatter chart. Here are the instructions on how […]
This tutorial shows you how to add a horizontal/vertical line to a chart. Excel allows you to combine two types […]
The chart above is built using the NORM.DIST function and is called Normal Distribution or Bell Curve chart. This curve is often […]
The scatter chart is great for charting numeric values in pairs, for example, coordinates. It lets you compare multiple data […]
Excel categories
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.