【问题标题】:Type Mismatch error when setting source data for a chart equal to an array将图表的源数据设置为等于数组时出现类型不匹配错误
【发布时间】: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

我不明白类型不匹配的来源。我非常感谢任何见解,谢谢。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    SetSourceData 接受 Range 作为源参数的数据类型,而不是数组。这在 https://msdn.microsoft.com/en-us/library/office/ff841196.aspx 的 MSDN 文档中得到了证实。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2023-02-11
      • 2021-10-28
      相关资源
      最近更新 更多