不使用范围创建具有多个序列的条形图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不使用范围创建具有多个序列的条形图相关的知识,希望对你有一定的参考价值。
我想知道是否有一种方法可以编写一个不使用Range就能创建具有多个系列(取决于输入)的条形图的宏?为了使它现在能够正常工作,在可以使用它之前,将我的输出写在一张纸上。这感觉非常不必要。还有其他方法吗?
Sub CreateBarGraph()
Set table = Database_U.ListObjects("Table_U") '
Dim ColumnProjektID As String, ColumnVarde As String
Dim Varde As Variant, ProjektID As Variant
ColumnProjektID = "ProjektID"
ColumnVarde = "Varde"
Dim sheet As Worksheet
Set sheet = GraphSheet
Dim cht As ChartObject
Dim i As Integer
i = 1
For Each currentRow In table.ListRows
'The return value consist of a name with letters
Set ProjektID = RetrieveProjektID(currentRow, ColumnProjektID)
GraphSheet.Cells(i, 2) = ProjektID
'The return value consist of a digit/number
Set Varde = RetrieveVarde(currentRow, ColumnVarde)
GraphSheet.Cells(i, 3) = Varde
i = i + 1
Next
Set cht = GraphSheet.ChartObjects.Add(Left:=20, Width:=800, Top:=20, Height:=500)
Dim rng As range
'1/2 of the below feels like a waste of space. I would like to find a
'way to add the source of the data (and create multiple series)
'without writing out the values on a sheet
For j = 1 To i
Set rng = GraphSheet.range(Cells(i, 2), Cells(i, 3))
cht.chart.SeriesCollection.Add Source:=rng
Next i
End Sub
是否有获得相同结果的替代方法?感谢您的帮助!
答案
A ChartObject本质上是一个包装Chart的Shape。您需要在ChartObject中包含的图表的SeriesCollection中添加NewSeries。
短代码
ActiveSheet.ChartObjects(1).Chart.SeriesCollection.NewSeries.Values = Array(10, 5, 21, 54, 76, 12)
演示
Sub Test() Dim ChartObject As ChartObject Dim Chart As Chart Set ChartObject = ActiveSheet.ChartObjects(1) Set Chart = ChartObject.Chart Dim Series As Series Set Series = Chart.SeriesCollection.NewSeries Series.Values = Array(10, 5, 21, 54, 76, 12) End Sub
这里是如何格式化系列'画线Series.Format.Line.ForeColor.RGB = vbRed'为内部填充物上色Series.Format.Fill.ForeColor.RGB = vbMagenta
附录
您可以分配一个这样的类别名称数组:
Dim Axis As Axis
Set Axis = Chart.Axes(xlCategory)
Axis.CategoryNames = myArray2
以上是关于不使用范围创建具有多个序列的条形图的主要内容,如果未能解决你的问题,请参考以下文章