+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Forum Guru jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    1,996

    Post Change chart type by using data validation

    Hi,

    Am I at all close on this.

    I have one chart and I want to cycle through 4 different types of charts using data validation.

    I used the macro recorder to get some of the syntax and added it under the Case “Ratio” to test, but not getting anything to happen.

    Can somebody help me out with the right way to go here?

    Code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Address(False, False) = "C3" Then
    
        ActiveSheet.ChartObjects("Chart 1").Activate
        
        Select Case Range("C3").Value
            
            Case "Linear"
            
    '        .Type = xlLinear
            
            Case "Power"
            
    '        .Type = xlPower
            
            Case "Ratio"
                ActiveChart.ChartTitle.Text = "Ratio"
                ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
                ActiveChart.SeriesCollection(1).Trendlines(1).Select
                Selection.Type = xlLogarithmic
            
            Case "Parabola"
    '            With Selection
    '                .Type = xlPolynomial
    '                .Order = 2
    '            End With
            
        End Select
    End If
    End Sub
    Last edited by jeffreybrown; 11-27-2009 at 04:45 PM. Reason: Follow-up

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Change chart type by using data validation

    I confess I know little of working with Charts in VBA but I would point out that you need to use the Change event rather than SelectionChange event - ie you want the code to fire whenever C3 is altered.

    So instead of:

    Code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Address(False, False) = "C3" Then
    ...
    End If
    End Sub
    you would use

    Code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Intersect(Target,Range("C3")) Is Nothing Then Exit Sub
    ...
    End Sub
    Other than that I can't really help much - it's not clear to me whether your trying to add and amend trend line types or the underlying chart type - it might help if you posted a sample file and perhaps provided examples of each chart that you wish to generate via the validation list.

    Hopefully Andy and co. will be able to help you resolve.

    (Probably worth just confirming that you are definitely using XL2007 - ie I suspect Charting options differ slightly between 2007 and predecessors)

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Change chart type by using data validation

    You say chart type although the code would suggest you are changing Trendline type.

    If it's trendline then you can do it all with formula and multiple data series.

    Can you post example workbook.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Guru jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    1,996

    Post Re: Change chart type by using data validation

    Hi Andy & DonkeyOte,

    Thank you for taking the time to help me here.

    Yes I am trying to change the trendline for an existing chart. Attached is an example workbook.
    Attached Files Attached Files

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Change chart type by using data validation

    Additional series on data sheet.

    There are 4 new series in the chart. Each only displays a trendline.
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  6. #6
    Forum Guru jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    1,996

    Post Re: Change chart type by using data validation

    Thank you so much Andy...perfect.

  7. #7
    Forum Guru jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    1,996

    Post Re: Change chart type by using data validation

    Okay now that I am looking back at what you provided Andy, for the life of me I cannot figure out how the chart knows to change the trendline. I don't see how the change is passed to the chart other than you added the four series and the values that are NA() are invisible to the chart. Any thoughts?

  8. #8
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,137

    Re: Change chart type by using data validation

    If I've got it right, Andy created four new series, each of which is the exact same data, and each has a diffferent type of trendline. Selecting a type of trendline makes three of the series #N/A, making it and its trendline disappear, leaving the selected one remaining.

    Andy is a very clever boy.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Guru jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    1,996

    Post Re: Change chart type by using data validation

    Yes you've got it right that's how he set in up, but again still don't understand how the data left after selecting a particular chart knows what trendline to draw?

  10. #10
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,137

    Re: Change chart type by using data validation

    Each of the series has a trendline -- four series, four trendlines. Only one series and its trendline are displayed at any given time.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Forum Guru jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    1,996

    Post Re: Change chart type by using data validation

    Thanks so much

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    10,944

    Re: Change chart type by using data validation

    Shg's explanation is correct.

    The only extra thing I did was to format the extra 4 series to have no data markers. The data markers are from your original series, which now has no trendline.
    Cheers
    Andy
    www.andypope.info

  13. #13
    Forum Guru jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    1,996

    Post Re: Change chart type by using data validation

    Thank you Shg and Andy it is all making sense now.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0