首页 > 解决方案 > Chart.SetSourceData 方法失败

问题描述

我在向工作表中添加图表时遇到问题。问题出在 .SetSourceData 方法上,我在这里和其他论坛尝试了各种解决方案,但没有任何效果。你能告诉我它是什么吗?

错误是:Method'SetSourceData' of object'Chart' failed

顺便说一句,范围已正确定义,已使用 myRng.Select 行进行了检查

这是代码

'adding the chart
Dim chrt As Chart
Dim myRng As Range
Set chrt = ws.Shapes.AddChart.Chart
Set myRng = Union(ws.Range(Cells(19 + pt.RowRange.Rows.Count, 5), Cells(19 +     
pt.RowRange.Rows.Count, 6)), ws.Range(Cells(20 + pt.RowRange.Rows.Count, 3), 
Cells(18 + 2 * (pt.RowRange.Rows.Count), 6)))
'myRng.Select
'Set chrt = ws.ChartObjects.Add.Chart
'chrt.Activate
    With chrt
    'For j = chrt.SeriesCollection.Count To 1 Step -1
    '    chrt.SeriesCollection(j).Delete
    'Next j
    '.SetSourceData Source:=ws.Range(Cells(19 + pt.RowRange.Rows.Count, 4), Cells(18 + 2 * (pt.RowRange.Rows.Count), 6)), PlotBy:=xlRows
    '.SetSourceData Source:=myRng, PlotBy:=xlRows
    .ChartType = xl3DColumnClustered
    '.SetSourceData Source:=Union(ws.Range(Cells(19 + pt.RowRange.Rows.Count, 5), Cells(19 + pt.RowRange.Rows.Count, 6)), ws.Range(Cells(20 + pt.RowRange.Rows.Count, 3), Cells(18 + 2 * (pt.RowRange.Rows.Count), 6))), PlotBy:=xlRows
    '.SetSourceData Source:=ws.Range(Cells(19 + pt.RowRange.Rows.Count, 4), Cells(18 + 2 * (pt.RowRange.Rows.Count), 6)), PlotBy:=xlRows
    '.SetSourceData Source:=myRng, PlotBy:=xlRows
    .Parent.Name = "Wykres_LDP"
    .ChartArea.Left = ws.Cells(20 + pt.RowRange.Rows.Count, 9).Left
    .ChartArea.Top = ws.Cells(20 + pt.RowRange.Rows.Count, 9).Top
    .ChartArea.Height = ws.Range(Cells(20 + pt.RowRange.Rows.Count, 9), Cells(45 + pt.RowRange.Rows.Count, 9)).Height
    .ChartArea.Width = 800
    .Parent.Placement = xlMove
    '.SetSourceData Source:=Union(ws.Range(Cells(19 + pt.RowRange.Rows.Count, 5), Cells(19 + pt.RowRange.Rows.Count, 6)), ws.Range(Cells(20 + pt.RowRange.Rows.Count, 3), Cells(18 + 2 * (pt.RowRange.Rows.Count), 6))), PlotBy:=xlRows
    .ChartColor = 10
    .ChartGroups(1).GapWidth = 75
    .SetElement (msoElementLegendBottom)
    .Legend.Font.Size = 12
    .Legend.Font.Bold = True
    .SetElement (msoElementChartTitleAboveChart)
    .ChartTitle.Text = "Liczba Dni Promocji - Wykres"
    .ChartTitle.Font.Bold = True
    .ChartTitle.Font.Size = 16
    'With .Axes(xlCategory, xlPrimary)
        ''.CategoryType = xlTimeScale
        '.MinimumScale = DateValue("01.01." & Year1)
        '.MaximumScale = DateValue("01.12." & Year2)
        ''.BaseUnitIsAuto = True
        '.BaseUnit = xlMonths '1
        '.MajorUnit = 1
        '.MajorUnitScale = xlMonths
        '.TickLabels.Orientation = xlUpward
        '.TickLabels.NumberFormat = "mmmm yy"
        '.TickLabels.Font.Bold = True
        '.TickLabels.Font.Size = 11
    'End With
End With

标签: vbaexcelexcel-charts

解决方案


推荐阅读