Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Multi choice data source select for a chart

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location

    Multi choice data source select for a chart

    I am wanting to make a multi choice dropdown to select a data range for the Primary series in a chart, but the following returns an error 424 object required. What have I done wrong?

    Data Sources are named tables (tblCharlevilletemps, tbleDalbyTemps, tblToowoombaTemps, tblWarwickTemps) and consist of three columns A:C on 4 sheets named (Charleville, Dalby, Toowoomba and Warwick), respectively. Similar to the other threads where a chemical was a multi select choice as the secondary axis.

    Sub Create_Dynamic_Chart()
    Dim sht As Worksheet
    Dim chrt As Chart
    Dim data_rng As Range
    Set sht = ActiveSheet
    Set data_rng = Range("A2").Value
    Select Case dRange
        Case Range("A2").Value = "Charleville"
            data_rng = tlCharlevilleTemps
        Case Range("A2").Value = "Dalby"
            data_rng = tblDalbyTemps
        Case Range("A2").Value = "Toowoomba"
            data_rng = tblToowoombaTemps
        Case Range("A2").Value = "Warwick"
            data_rng = tblWarwickTemps
    End Select
    Set chrt = sht.Shapes.AddChart2(Style:=-1, Width:=900, Height:=200, Left:=Range("G2").Left, Top:=Range("G2").Top).Chart
    With chrt
        .SetSourceData Source:=data_rng
        .ChartType = xlLineStacked
        .ChartTitle.Text = Range("A2").Value & " Min & Max Temps"
        .SetElement msoElementDataLabelOutSideEnd
        .SetElement msoElementPrimaryValueGridLinesMajor
        .SetElement msoElementLegendBottom
        .SetElement msoElementPrimaryCatergoryAxixTitleBelowValue
    End With
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    VBAX Regular
    Joined
    Sep 2023
    Posts
    99
    Location
    There is a typo here, missing the b in tblCharleville:
    data_rng = tlCharlevilleTemps
    likely unrelated to your issue.

  3. #3
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Good spot jdelano,

    I have included that in the below. I got the below to work after having a play (not all of your setting for the chart are included and i set it to xlLine):
    Sub Create_Dynamic_Chart()
        Dim sht As Worksheet
        Dim chrt As Chart
        Dim data_rng As Range
        Dim dRange As Range
        
        Set sht = ActiveSheet
        Set dRange = Range("A2") ' dropdown
        
        Select Case dRange.Value
            Case "Charleville"
                Set data_rng = Range("tblCharlevilleTemps")
            Case "Dalby"
                Set data_rng = Range("tblDalbyTemps")
            Case "Toowoomba"
                Set data_rng = Range("tblToowoombaTemps")
            Case "Warwick"
                Set data_rng = Range("tblWarwickTemps")
        End Select
        
        Set chrt = sht.Shapes.AddChart2(Style:=-1, Width:=900, Height:=200, Left:=Range("G2").Left, Top:=Range("G2").Top).Chart
        
        With chrt
            .SetSourceData Source:=Range(data_rng.Address(, , , 1))
            .ChartType = xlLine
            .SetElement (msoElementChartTitleAboveChart)
            .ChartTitle.Text = Range("A1").Value & " Min & Max Temps"
        End With
    End Sub
    You could also do the same with less code:
    Sub Create_Dynamic_Chart2()
        Dim chrt As Chart
        
        Set chrt = ActiveSheet.Shapes.AddChart2(Style:=-1, Width:=900, Height:=200, Left:=Range("G2").Left, Top:=Range("G2").Top).Chart
        With chrt
            .SetSourceData Source:=Range(Range("tbl" & Range("A2").Value & "Temps").Address(, , , 1))
            .ChartType = xlLine
            .SetElement (msoElementChartTitleAboveChart)
            .ChartTitle.Text = Range("A2").Value & " Min & Max Temps"
        End With
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Yes you are correct, it is a typo but it still doesn't like the line

    set data_rng =  Range("A2").Value
    and.... its the simplest things that you cant see in the forest. Corrected the Range("A2").value

    and now it says
    the first line is incorrect as its undefined

    Sub Create_Dynamic_Chart()
    Dim sht As Worksheet
    Dim chrt As Chart
    Dim data_rng As Range
    Set sht = Sheet1
    Set data_rng = Worksheet("Chart").Range("A2").Value
    Select Case dRange
        Case Range("A2").Value = "Charleville"
            data_rng = tblCharlevilleTemps
        Case Range("A2").Value = "Dalby"
            data_rng = tblDalbyTemps
        Case Range("A2").Value = "Toowoomba"
            data_rng = tblToowoombaTemps
        Case Range("A2").Value = "Warwick"
            data_rng = tblWarwickTemps
    End Select
    Set chrt = sht.Shapes.AddChart2(Style:=-1, Width:=900, Height:=200, Left:=Range("G2").Left, Top:=Range("G2").Top).Chart
    With chrt
        .SetSourceData Source:=data_rng
        .ChartType = xlLineStacked
        .ChartTitle.Text = Range("A2").Value & " Min & Max Temps"
        .SetElement msoElementDataLabelOutSideEnd
        .SetElement msoElementPrimaryValueGridLinesMajor
        .SetElement msoElementLegendBottom
        .SetElement msoElementPrimaryCatergoryAxixTitleBelowValue
    End With
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Quote Originally Posted by Aussiebear View Post
    and now it says
    the first line is incorrect as its undefined
    That is because you have the variable defined as a range but you are using Set to assign a value, you would need to remove the '.Value'

    See post 3 for another option
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Sorry Georgiboy, but your code allegedly fails at the
    .SetSourceData Source:=Range(Range("tbl" & Range("A2").Value & "Temps").Address(, , , 1))
    
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Have you checked the names of your tables, the typo may have been in your table name?

    See the attached file I have been playing with.
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Stop the train! Something is happening with the workbook. Now I'm getting an empty chart.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Have confirmed the table names are correct.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  10. #10
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    What happens with my attachment?

    Can you share the file you are working on?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  11. #11
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    In the morning, I shall post it.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    I'm in a state of confusion now..... so here it is.
    Attached Files Attached Files
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    For me it seems to work fine, however, I had to correct the typo:

    In your dropdown list you have 'Charelville' and in your code you have 'Charleville'.

    I also noted that the dates you have in the 'Dalby' tab don't really work and they are just integers.

    Lastly I would recommend using tables for your data in the four tabs rather than named ranges, this way when you add new data, the table will expand and so will the range for the chart.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  14. #14
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Okay, I have converted named ranged to Tables, Corrected the typo for Charleville, and corrected the integers to proper dates.

    Your code works correctly, only the date labels at the bottom are positioned at the Zero level yet the charts being created have a minimum level of -10. How can I fix that issue?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  15. #15
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    Here is another idea:

    Rather than create a new chart each time, just change the data source of the chart that already exists on the sheet. In the below example the chart I was working with was named "Chart 4". This way you can format the chart and the changes will stick.
    Sub Create_Dynamic_Chart2()
        Dim sht As Worksheet
        Dim chrt As ChartObject
        Dim data_rng As Range
        Dim dRange As Range
        
        Set sht = ActiveSheet
        Set dRange = Range("A2") ' dropdown
        
        Select Case dRange.Value
            Case "Charelville"
                Set data_rng = Range("tblCharlevilleTemps")
            Case "Dalby"
                Set data_rng = Range("tblDalbyTemps")
            Case "Toowoomba"
                Set data_rng = Range("tblToowoombaTemps")
            Case "Warwick"
                Set data_rng = Range("tblWarwickTemps")
        End Select
        
        Set chrt = sht.ChartObjects("Chart 4")
        With chrt.Chart
            .SetSourceData data_rng
            .ChartTitle.Text = Range("A2").Value & " Min & Max Temps"
        End With
    End Sub
    In answer to your question above:
    Lower the labels on the Y axis: Right click on the date axis, select 'Format Axis', in the 'Labels' part, change the dropdown to the 'Low' option.

    If you format the chart with the above and also use the code above, the changes on the chart will stick rather than resetting each time you add a new chart.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  16. #16
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    If you can share your updated file with the tables etc. I can have a look at the Chem type and period, if you wish?
    Last edited by georgiboy; 04-16-2024 at 03:14 AM. Reason: typo
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  17. #17
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Okay.
    Attached Files Attached Files
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  18. #18
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    I had started working on this before you sent your latest file so it comes without the additional sheets you added. I hope you can follow what I have done so that you can add the extra sheets yourself, if not then you can always shout. It maybe that someone has a better solution, anyhow, I hope the attached will help 'you' or others to help you get to the final product.
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  19. #19
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Thank you georgiboy. Works very well. I will include the data for Roma, Goondiwindi, Kingaroy, & Gatton to build a database. I have also included a visual display below the chart to reflect SDS information, so it looks good from my perspective.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  20. #20
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    My interpretation.
    I also added the period selection.
    It takes only 1 worksheet_change macro.
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •