Excel multiple dynamic graphs

后端 未结 1 601
自闭症患者
自闭症患者 2020-12-22 04:54

I have a series of data which I\'d like to plot into a few graphs. The data is ordered by date and extracted data tagged with a TRUE condition next to it based on prior cond

相关标签:
1条回答
  • 2020-12-22 05:17

    This is doable. Creating charts dynamically is one of those routines that you should put away in a library for future reference. I have done so and the code is below. The code will create a chart based on x/y ranges and a location. The location allows the charts to be arranged in a grid as they are created. You will have to wrangle your ranges so that you can give the sub below the inputs it needs. This should just be a matter of iterating through and tracking which charts to create.

    The only key steps to this are using ChartObjects.Add to create a new chart (with positioning data) and then SeriesCollection.NewSeries to add a series to the chart.

    You can call this code several times with location incrementing to create the charts you want and put them in the grid.

    Sub CreateChartFromRange(xval As Range, yval As Range, location As Integer)
    
        Dim height As Double, width As Double
        height = 300
        width = 300
    
        Dim columns As Integer
        columns = 3
    
        'assume active sheet
        Dim cht_obj As ChartObject
        Set cht_obj = ActiveSheet.ChartObjects.Add( _
            (location Mod columns) * width, _
            (location \ columns) * height, _
            width, _
            height)
    
        Dim ser As Series
        Set ser = cht_obj.Chart.SeriesCollection.NewSeries
    
        ser.Values = yval
        ser.XValues = xval
    
        'assume XY scatter type
        ser.ChartType = xlXYScatter
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题