How to assign XValues for excel chart using VBA

前端 未结 2 606
鱼传尺愫
鱼传尺愫 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:52

    You cannot use named ranges for .XValues or .Values, but you can change the .Formula property by replacing the series formula

    For more information on editing the series formula see http://peltiertech.com/change-series-formula-improved-routines/

    Note of caution, there is a bug in Excel 2013 that prevents you from using named ranges starting with "R" or "C" when using VBA to change the series formula; see http://answers.microsoft.com/en-us/office/forum/office_2013_release-excel/named-range-use-in-chart-series-formulas-causes/c5a40317-c33f-4a83-84db-0eeee5c8827f/?auth=1&rtAction=1466703182593

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题