How to assign XValues for excel chart using VBA

前端 未结 2 623
鱼传尺愫
鱼传尺愫 2020-12-22 00:49

I have this VBA function for drawing charts in Excel 2013:

Sub DrawChart2(obj_worksheetTgt As Worksheet, ByVal XLabels As Range, ByVal DataValues As Range, B         


        
2条回答
  •  暖寄归人
    2020-12-22 00:59

    Before you can set the Values and XValues of a series, you will need to add the series first. This is simple to do using the SeriesCollection.NewSeries method as show below:

    With ActiveSheet.ChartObjects.Add(a, b, 900, 300) ' Left, Top, Width, Height
        With .Chart
            .ChartType = xlBarClustered
    
            ' need to add the series before you can assign the values/xvalues
            ' calling the "NewSeries" method add one series each time you call it.
    
            .SeriesCollection.NewSeries
    
            ' now that the series is added, you may assign (not set) the values/xvalues
    
            .SeriesCollection(1).XValues = XLabels
            .SeriesCollection(1).Values = DataValues
        End With
    End With
    

提交回复
热议问题