[英]Type Mismatch error when setting source data for a chart equal to an array
我需要使用一個范圍內的值填充數組,然后將其用作圖表的源數據。 我試過將數組設置為變量,並且設置了很長時間,但似乎都無法正常工作。 每次我嘗試運行代碼時,在.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
我不知道類型不匹配的來源。 我非常感謝您的任何見解,謝謝。
SetSourceData接受Range作為源參數的數據類型,而不是數組。 這已在MSDN文檔中得到確認, 網址為https://msdn.microsoft.com/zh-cn/library/office/ff841196.aspx )。
您需要將數據包括在工作表范圍內,並將其用作源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.