设置图表的源数据等于数组时,请input“不匹配”错误

我需要用范围内的值填充数组,然后我想使用这个数组作为图表的源数据。 我已经尝试设置数组作为变体,只要似乎都没有工作。 每次我尝试运行代码时,我都会在.SetSourceData:= PlotRangeBar行中得到types不匹配错误。 以下是我到目前为止:

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 

我不明白types不匹配是从哪里来的。 我非常赞赏任何见解,谢谢。

SetSourceData接受Range作为源参数的数据types,而不是数组。 这在MSDN文档中得到确认, url为:https://msdn.microsoft.com/en-us/library/office/ff841196.aspx )。

您需要将数据包含在工作表范围内,并将其用作源。