问题描述
我想在excel宏中创建折线图.我在excel中有下表.
I want to create line chart in excel macro. I have below table in excel.
第一条折线图包含单元格A和B中的值范围.第二条折线图包含单元格A和C中的值范围.第三条折线图包含单元格A和D中的值范围.
First Line Chart contains range of values from Cell A and B. Second Line Chart contains range of values from Cell A and C. Third Line Chart contains range of values from Cell A and D.
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("A1:D3")
ActiveChart.ApplyDataLabels
ActiveChart.ChartType = xlLine
以上代码仅产生一个折线图,范围从A1到D4.
Above code produces only one Line Chart for range A1 to D4.
推荐答案
这是一个很好的问题,宏记录器可以为如何回答提供很好的提示.您只需要定义另一个系列集合.这是宏记录器未经修改即可产生的结果.
This is a good question that the macro recorder can provide a good hint on how to answer. You just need to define another series collection. Here is what the macro recorder produces without modification.
Sub Macro1()
Range("A1:D2").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$D$2")
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "=Sheet1!$A$3"
ActiveChart.SeriesCollection(2).Values = "=Sheet1!$B$3:$D$3"
End Sub
这是不使用select的方式
Here is how I would do it without using select
Sub createChart()
Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
Dim chrt As Chart
Set chrt = wks.Shapes.AddChart.Chart
With chrt
.ChartType = xlLine
.SetSourceData Source:=Range("Sheet1!$A$1:$D$2")
.SeriesCollection.NewSeries
.SeriesCollection(2).Name = "=Sheet1!$A$3"
.SeriesCollection(2).Values = "=Sheet1!$B$3:$D$3"
End With
End Sub
循环
Sub createChartWithLoop()
Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
Dim chrt As Chart
Set chrt = wks.Shapes.AddChart.Chart
Dim chartRange As Range
Set chartRange = wks.Range("A1:A4")
With chrt
.ChartType = xlLine
.SetSourceData Source:=Range(wks.Range("A" & chartRange.Row & ":D" & chartRange.Row).Address)
For i = chartRange.Row + 1 To chartRange.Rows.Count
.SeriesCollection.NewSeries
.SeriesCollection(i).Name = wks.Cells(i, 1)
.SeriesCollection(i).Values = wks.Range("B" & i & ":D" & i)
Next i
End With
End Sub
编辑-根据每个行数据创建不同的图表
EDIT - Create different charts from each row of data
Sub createDifferentCharts()
Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
Dim chartRange As Range
Set chartRange = wks.Range("A1:A4")
For i = chartRange.Row To chartRange.Rows.Count
With wks.Shapes.AddChart.Chart
.ChartType = xlLine
.SetSourceData Source:=Range(wks.Range("A" & i & ":D" & i).Address)
.Parent.Left = 20 + i * 50
.Parent.Top = 40 + i * 50
End With
Next i
End Sub
结果:
为每个列创建不同的图表,其中A列为标签:
Create different charts for each column of data with column A being the labels:
Sub createDifferentCharts()
Dim wks As Worksheet
Set wks = Worksheets("Sheet1")
Dim chartRange As Range
Set chartRange = wks.Range("A1:D4")
For col = chartRange.Column + 1 To chartRange.Columns.Count
With wks.Shapes.AddChart.Chart
.ChartType = xlLine
.SetSourceData Source:=Range(wks.Cells(1, 1).Address)
.SetSourceData Source:=wks.Range("A1:A4," & Range(Cells(chartRange.Row, col), Cells(chartRange.Rows.Count, col)).Address)
.Parent.Left = col * 30
.Parent.Top = col * 30
End With
Next col
End Sub
这篇关于Excel宏以创建折线图,中间不包括列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!