Manipulating Charts Without Activating

  • a simple macro using the recorder yields:


    Code
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "=1+1"


    this, of course, can be made more efficient:


    Code
    Range("A1").FormulaR1C1 = "=1+1"


    instead of selecting the range then manipulating the active range, the action is done directly on the range in one step.



    now, charts...


    with recorder:


    Code
    ActiveSheet.ChartObjects("Chart 1").Activate
        ActiveChart.ChartArea.Select
        ActiveChart.ChartType = xlBarClustered


    i can not find a way to combine this properly so that the action is done without the chart being 'activated'.


    the ideal solution would be something like this (conceptually):


    Code
    Dim mychart As Chart
        Set mychart = Sheet1.ChartObjects("chart 1")
        With mychart
            .ChartType = xlBarClustered
            .PlotBy = xlColumns
    ... etc.
        End With


    i suppose i'm not doing the set part correctly. what's the correct syntax?


    also, i'm open to other ideas to achieve this. does not have to be like above.


    thanks!

  • Re: Manipulating Charts Without Activating


    Try this:

    Code
    Sub MyTest()
    Dim mychart As ChartObject
    Set mychart = Sheet1.ChartObjects("chart 1")
    With mychart.Chart
        .ChartType = xlBarClustered
        .PlotBy = xlColumns
        
    End With
    End Sub
  • Re: Manipulating Charts Without Activating


    Or using the Chart variable
    [vba]Dim mychart As Chart
    Set mychart = Sheet1.ChartObjects("chart 1") .chart
    With mychart
    .ChartType = xlBarClustered
    .PlotBy = xlColumns
    ... etc.
    End With [/vba]

    [h4]Cheers
    Andy
    [/h4]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!