我有一个以编程方式生成此图表的任务:
到目前为止,我找到了这些resources 和MSDN Docs用于使用VBA生成图表。
这是我目前的代码:
Private Sub CommandButton1_Click()
Sheet1.Select
ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes(1).Top = 10
ActiveSheet.Shapes(1).Left = 10
ActiveChart.ChartType = xlLineMarkers
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=Range("Table1")
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = Sheet1.Range("B2").Value
End Sub
然而,我并没有完全得到我需要的输出。
这是一个示例数据表:
注意:
答案 0 :(得分:2)
嗯,以下内容即将结束。你需要改变范围(或者设置它以动态找到它 - 这是另一个问题)并设置标题所在位置的单元格范围等但我认为这应该可以帮助你:
Excel 2013版:
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
With ActiveChart
' Set the source
.SetSourceData Source:=Range("Sheet1!$A$1:$D$6")
' Set the title
.ChartTitle.Text = Sheet1.Range("B1").Value
' Format the labels to 45 degrees
.Axes(xlCategory).TickLabels.Orientation = 45
' Set Line 2 to Orange dash, no marker
With .FullSeriesCollection(2).Format.Line
.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.DashStyle = msoLineDash
.Parent.Parent.MarkerStyle = -4142
'add other formatting here
End With
' Set Line 3 to Orange dash, no marker
With .FullSeriesCollection(3).Format.Line
.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.DashStyle = msoLineDash
.Parent.Parent.MarkerStyle = -4142
'add other formatting here
End With
End With
我是通过录制宏然后添加With
来删除.Activechart
的不必要使用来获得此功能的。你可以随时Set
如果你需要稍后再参考它,那又是另一个问题。如果你想进一步改进,你也可以创建一个循环来取出.FullSeriesCollection()
的一段代码。
Excel 2007/2010版本:(使用循环和可见性解决方法以允许颜色更改)
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.ApplyLayout (1)
.SetSourceData Source:=Range("'Sheet1'!$A$1:$D$6")
.ChartType = xlLineMarkers
.Axes(xlCategory).TickLabels.Orientation = 45
.ChartTitle.Text = Sheet1.Range("B1").Value
For scoln = 2 To 3
With .SeriesCollection(scoln)
.Format.Line.Visible = False
.Format.Line.Visible = True
.Format.Line.ForeColor.RGB = 683236
.Format.Line.DashStyle = msoLineSysDash
.MarkerStyle = -4142
End With
Next
With .SeriesCollection(1)
.MarkerStyle = 8
.MarkerSize = 8
End With
End With