Solved

Change data on Excel Graph from dropdown

Posted on 2016-09-23
4
72 Views
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
Template_4.xlsx
0
Comment
Question by:zerog
[X]
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
4 Comments
 
LVL 3

Expert Comment

by:suman_g4
ID: 41812463
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41812522
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
 
LVL 81

Accepted Solution

by:
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:
=$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
 

Author Comment

by:zerog
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

733 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