Excel 2010 .SetSourceData vba图表

时间:2013-06-06 19:54:10

标签: excel vba excel-vba charts

早上好,

我在为堆积条形图选择.SetSourceData的范围时遇到问题。

我的范围是不连续的。开始日期在G列中,其余数字在V到AI列中计算。

目前,这只是通过AI绘制列V,并且使用列G作为水平轴标签。我尝试将水平轴标签范围(列E)添加为Union的第一个.range参数,但随后显示E&列。 F(不确定F来自哪里)作为hoz。轴标签。 G栏无处可寻。有没有办法用SetSourceData做到这一点,还是我必须为所有15个系列单独设置SeriesCollection?

见下面的代码:

Sub Create_Chart()
'
' Create_Chart Macro
'
Dim ganttChart As ChartObject
Dim sourceRange As Range

With Sheets("Projects")
    Set sourceRange = Union(.Range("G1:G70"), .Range("V1:AI70"))
End With

Set ganttChart = ActiveSheet.ChartObjects.Add(100, 50, 200, 200)

With ganttChart
With .Chart
    .ChartType = xlBarStacked
    .Legend.Delete
    .Parent.Width = 1224
    .Parent.Height = 828
    .SetSourceData Source:=sourceRange, PlotBy:=xlColumns
    .Location Where:=xlLocationAsNewSheet

    With .Axes(xlValue)
        '.MinimumScale = 41428
        '.MaximumScale = 41607
        .MajorUnit = 7
        .TickLabels.NumberFormat = "m/d"
        .TickLabels.Font.Size = 6
        .TickLabels.Font.Name = "Calibri"
    End With
    With .Axes(xlCategory)
        .ReversePlotOrder = True
        .TickLabelSpacing = 1
        .TickLabels.NumberFormat = "@"
        .TickLabels.Font.Size = 6
        .TickLabels.Font.Name = "Calibri"
    End With
End With
End With
End Sub

1 个答案:

答案 0 :(得分:0)

您可以尝试在插入图表之前选择源范围。 Excel可能会使用正确的范围开始,因此您无需定义源数据。

另一种方法是一次添加一个系列:

Set ganttChart = ActiveSheet.ChartObjects.Add(100, 50, 200, 200)

' clean up first
' i declared as long
For i = 1 to ganttChart.SeriesCollection.Count
    ganttChart.SeriesCollection(1).Delete
Next

' rColumn declared as range
For Each rColumn In Sheets("Projects").Range("V1:AI70").Columns
    With ganttChart.NewSeries
        .Values = rColumn
        .XValues = Sheets("Projects").Range("G1:G70")
    End With
Next