使用数组创buildVBA图表

我正在尝试创build一个使用VB6的Excel图表。 而不是提供一个Excel范围即时尝试提供一个数组。 即时通讯得到一个错误。 这是即时通讯工作的代码

Private Sub CreateChart(Optional ByVal ChartTitle As String _ , Optional ByVal xAxis As Excel.Range _ , Optional ByVal yAxis As Excel.Range _ , Optional ByVal ColumnName As String _ , Optional ByVal LegendPosition As XlLegendPosition = xlLegendPositionRight _ , Optional ByVal rowIndex As Long = 2 _ , Optional ByRef ChartType As String = xlLineMarkers _ , Optional ByVal PlotAreaColorIndex As Long = 2 _ , Optional ByVal isSetLegend As Boolean = False _ , Optional ByVal isSetLegendStyle As Boolean = False _ , Optional ByVal LegendStyleValue As Long = 1) Const constChartLeft = 64 Const constChartHeight = 300 Const constChartWidth = 700 Dim xlChart As Excel.ChartObject Dim seriesCount As Long Dim ColorIndex As Long Dim j As Long With mWorksheet .Rows(rowIndex).RowHeight = constChartHeight Set xlChart = .ChartObjects.Add(.Rows(rowIndex).Left, .Rows(2).Top, constChartWidth, constChartHeight) End With With xlChart.chart .ChartType = ChartType .SetSourceData Source:=marrayPOClient, PlotBy:=marrayPOSKU .SeriesCollection(1).XValues = marrayPOClient .HasTitle = True .Legend.Position = LegendPosition .Legend.Font.Size = 7.3 .Legend.Font.Bold = True .Legend.Border.LineStyle = xlNone .ChartTitle.Characters.Text = ChartTitle .ChartTitle.Font.Bold = True .Axes(xlValue).TickLabels.Font.Size = 8 ' yAxis Labels .Axes(xlCategory).TickLabels.Font.Size = 8 ' xAxis Labels .PlotArea.Interior.ColorIndex = PlotAreaColorIndex .PlotArea.Interior.ColorIndex = 15 .PlotArea.Interior.PatternColorIndex = 1 .PlotArea.Interior.Pattern = xlSolid End With End Sub 

是否有可能使用数组的图表。 如果可能,我的错误是什么。

正如Mat's Mug所说的, SetSourceData需要一个Range ,但是你可以用另一种方法来获得结果

更换

 .SetSourceData Source:=marrayPOClient, PlotBy:=marrayPOSKU 

 .SeriesCollection.NewSeries .SeriesCollection(1).Values = marrayPOClient 

这将创build一个没有源的新系列,然后将数组分配为系列值

Chart.SetSourceData需要一个Range对象作为它的Source参数,而一个XlRowCol枚举值则是它的PlotBy参数。

假设 marrayPOClientmarrayPOSKU都是数组作为他们的名字暗示(你没有显示它们在哪里声明和如何分配,所以我们不知道它们的types或价值),但你需要提供一个第一个参数的Range ,以及第二个参数的xlColumnsxlRows (可选)。