Author: Oscar Cronquist Article last updated on December 16, 2019

Highlight group of values in a xy 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.

Dynamic data labels in a chart1

Add a Drop Down List

Highlight group of values in a xy scatter chart programmatically 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:

  1. Select cell I18.
  2. Go to tab "Data" on the ribbon.
  3. Click on the "Data Validation" button.
  4. Select List.
  5. Type Africa, Asia, Europe, North America, South America in Source:
    data validation dynamic data labels
  6. Click OK to create a Drop Down List

Insert a scatter chart

Highlight group of values in a xy scatter chart programmatically

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.

  1. Select the numbers in table table1
    table - dynamic data labels
  2. Go to tab "Insert".
  3. Click the "Scatter" chart button.
  4. 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

                '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
            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.

  1. Right-click on the sheet name.
  2. Click on "View Code".
    table - vb editor1
  3. Paste code (see below) to sheet module.

    table - vb editor

  4. 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.