【发布时间】:2015-07-10 05:48:00
【问题描述】:
我需要用一个范围内的值填充一个数组,然后我想使用这个数组作为图表的源数据。我已经尝试将数组设置为变体并且只要并且似乎都不起作用。每次我尝试运行代码时,都会在 .SetSourceData:=PlotRangeBar 的行中收到类型不匹配错误。这是我目前所拥有的:
Dim XRangeBar As Range
Dim PlotRangeBar() As Variant
Dim PlotRange As Range
Set XRangeBar = ActiveWorkbook.Sheets(2).Range("B" & DataStart & ":B" & DataEnd)
i = 0
For Row = DataStart To DataEnd
If Cells(Row, UsedColTimesheet).FormulaR1C1 <> "0" And Cells(Row, UsedColTimesheet) <> vbNullString Then
ReDim Preserve PlotRangeBar(i)
PlotRangeBar(i) = Cells(Row, UsedColTimesheet).Value
i = i + 1
End If
Next
ActiveWorkbook.Sheets(Sheets.Count).Select
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.ChartType = xlColumnStacked
.SetSourceData Source:=PlotRangeBar 'Error occurs here
.SeriesCollection.NewSeries
.SeriesCollection(1).XValues = XRangeBar
.SetElement (msoElementChartTitleCenteredOverlay)
.ApplyLayout (1)
.ChartTitle.Text = ResourceName & " - Hours per project"
.Legend.Delete
.ChartStyle = 18
.ProtectSelection = True
End With
我不明白类型不匹配的来源。我非常感谢任何见解,谢谢。
【问题讨论】: