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

Expert Comment

ID: 41812463
LVL 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now