Solved

Change data on Excel Graph from dropdown

Posted on 2016-09-23
4
49 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
4 Comments
 
LVL 3

Expert Comment

by:suman_g4
ID: 41812463
0
 
LVL 48

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 80

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

705 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

12 Experts available now in Live!

Get 1:1 Help Now