使用数组创建VBA图表
我正在尝试使用vb6创建excel图表。我没有输入excel范围,而是尝试输入数组。我得到一个错误。 这是我正在编写的代码使用数组创建VBA图表,vba,excel,Vba,Excel,我正在尝试使用vb6创建excel图表。我没有输入excel范围,而是尝试输入数组。我得到一个错误。 这是我正在编写的代码 Private Sub CreateChart(Optional ByVal ChartTitle As String _ , Optional ByVal xAxis As Excel.Range _ , Optional ByVal yAxis As Excel.Range _
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
是否可以将数组用于图表。如果可能,我的错误是什么。要求其源
参数使用范围
对象,其绘图方式
参数使用XlRowCol
枚举值
我假设
marrayPOClient
和marrayPOSKU
都是其名称所暗示的数组(您没有显示它们的声明位置和分配方式,因此我们无法知道它们的类型或值),但您需要为第一个参数提供一个范围,并且可以选择,第二个参数可以是xlColumns
或xlRows
。正如Mat的杯子所说,SetSourceData
需要一个范围,但可以使用另一种方法获得结果
替换
.SetSourceData Source:=marrayPOClient, PlotBy:=marrayPOSKU
与
这将创建一个没有源的新序列,然后将数组指定为序列值,我将得到一个错误-告诉我们您得到的确切错误将非常有用。@Mat'sMug它说“需要对象”很好,这是一个开始。。。是否突出显示了一行代码或特定函数调用?@Mat'smugh这行代码中出现了错误。SetSourceData:=marrayPOClient,PlotBy:=marrayPOSKU
请注意,如果使用数组,它可以包含的数据量是有限的,因为你受到级数公式最大长度的限制,我也是这么想的。还有别的办法吗?我尝试使用chart.values
,但仍然失败。错误为“无效参数”
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = marrayPOClient