Change data on Excel Graph from dropdown

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
Template_4.xlsx
zerogAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

suman_g4Manager - ITCommented:
0
Rgonzo1971Commented:
Hi,

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

Regards
Template_4v1.xlsm
0
byundtMechanical EngineerCommented:

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:
=$B$2

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.
Template_4Q28971878.xlsx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zerogAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.