Change data on Excel Graph from dropdown

Posted on 2016-09-23
Last Modified: 2016-09-26
I have an Excel workbook with 4 sheets (Terms 1, 2, 3 and 4). The last sheet has a graph. How would I get the data that plots the graph to change based on the dropdown selection (i.e. either term 1, 2, 3 or4). Sample file attached
Question by:zerog
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 51

Expert Comment

ID: 41812522

pls try ( in the "graph" sheet module)
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("b2")) Is Nothing Then
        With Sheets(Replace((Sheets("graph").Range("B2").Value), " ", ""))
            Set chrtdata = .Range(.Range("B1"), .Range("E" & Rows.Count).End(xlUp))
            Me.ChartObjects("Chart 1").Chart.SetSourceData Source:=chrtdata
        End With
    End If
End Sub

Open in new window

LVL 81

Accepted Solution

byundt earned 500 total points
ID: 41814964

Brute force method

If you create some named ranges that use the INDIRECT function so the worksheet can vary, you can make your chart respond to selections in your data validation dropdown.

Before setting up the chart, I changed the names of the Term1 etc. worksheets so they matched the data validation dropdown in B2 (i.e. Term 1, Term 2, Term 3 & Term 4). I also put the following formula in cell B3 to return the number of students:
=COUNT('Term 1'!$C$5:$C$100)

To set up the chart, I created four named ranges called Target, Mark, Difference and Students. I did this using the Formulas...Name Manager menu item.
Target          =INDIRECT("'" & $B$2 & "'!$C$5:$C$" & ($B$3+5-1))
Mark             =INDIRECT("'" & $B$2 & "'!$D$5:$D$" & ($B$3+5-1))
Difference      =INDIRECT("'" & $B$2 & "'!$E$5:$E$" & ($B$3+5-1))
Students        =INDIRECT("'" & $B$2 & "'!$B$5:$B$" & ($B$3+5-1))

I then selected each series in turn on the chart, and changed the titles to be the fixed text Target %, Mark %, and Difference %.

I used the Chart Tools...Design...Select Data menu item to Edit the series values so they had a formula like:
='Name of workbook'!Target

Similarly, I clicked the Edit button for the Categories so they had a formula like:
='Name of workbook'!Students

Finally, I selected the text of the chart title, deleted it, and then clicked in the Formula bar to enter the following formula:

PivotChart is better way to do it

Having shown you the hard way to do what you want, I now want to suggest that you learn how to use the easy way that Microsoft offers. It is called a PivotChart. You will need to arrange your data on a single worksheet and add a column for the term number. But once you've done that, a PivotChart gives you the ability to select the term you want to see without need for the complex named ranges described previously.

When creating a PivotTable or PivotChart, it is best if the source data are located in a Table. I created one by selecting a cell in the data and then using the Insert...Table menu item. The big advantage of using a Table is that the associated PivotTable and/or PivotChart automatically grow and shrink as you add or delete data.

To create the PivotChart, I selected a cell in the Table, then used the Insert...PivotChart menu item. For simplicity, I chose to put the PivotChart on the same worksheet as its data.

The confusing part to people who haven't used PivotTable or PivotChart before comes with the next step--the Wizard. I did the following:
1. Drag Target % from the field list box at top to the Values box at bottom right. Repeat with Mark % and Difference %
2. Drag Term from the field list box at top to the Filters box below on the left
3. Drag Name from the field list box at top to the Axis (Categories) box at bottom left
4. Select the y-axis on the PivotChart and change the number format to Percentage with 0 decimal places
5. Use the Chart Tools...Design...Add Chart element menu item to add an Axis Title to the Primary Vertical axis. I chose "Percentage".
6. Click on the Filter at the top left of the chart, and choose one of the terms

I didn't bother with the remaining steps to duplicate your original chart. They won't be any different from what you had to do to format that original chart.

Author Comment

ID: 41815470
Thank you Rgonzo1971 and byundt
I decided to go with the non-macro version of the solution by byundt. Thank byundt, your explanation has taught me a lot for future solutions

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question