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.
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.
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.
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.
'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
'Use country name as Data Label text
.SeriesCollection(1).Points(r).DataLabel.Text = Range("Table1[Country]").Cells(r).Value
'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
'Disable error handling
On Error GoTo 0
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
Note, save your workbook with file extension *.xlsm (macro-enabled workbook) in order to keep the VBA code attached to the workbook.