使用VBA创建图表并删除边框

时间:2016-08-04 20:37:11

标签: excel vba charts border borderless

我有一个宏,可以为三个数据系列创建一个折线图。 Excel自动在图形周围添加边框,我鄙视这一点,但无法弄清楚如何删除它。我尝试过这些变体:

  • ActiveSheet.Shapes("Chart 1").Line.Visible = msoFalse
  • ChartArea.Border.LineStyle = xlNone

这是一个示例数据集:

            Data1   Data2   Data3
    2005    39      907     108
    2006    439     341     490
    2007    238     554     570
    2008    882     112     134
    2009    924     222     50
    2010    155     550     754
    2011    154     681     714
    2012    235     186     917

这是我目前的代码:

Sub MakeCharts2()

'save active sheet
Dim ActSheet As Worksheet
Set ActSheet = ActiveSheet
'save sheetname as string
Dim strSheetName As String
strSheetName = ActiveSheet.Name

ActSheet.Select

'insert chart
Range("A1:D9").Select
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range("A1:D9")
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Chart_" & strSheetName
ActiveChart.ChartArea.Select

'add title to chart
ActiveChart.ChartTitle.Select
Selection.Caption = "=" & strSheetName
' remove chart border: THIS IS WHERE I'M HAVING TROUBLE.
ActiveChart.ChartArea.Select
ActiveChart.Axes(xlValue).MajorGridlines.Select
ActiveSheet.Shapes("Chart 1").Line.Visible = msoFalse
Selection.Delete

'add vertical axis
ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Tons"

End Sub

1 个答案:

答案 0 :(得分:2)

使用

更改此行ActiveSheet.Shapes("Chart 1").Line.Visible = msoFalse

ActiveSheet.Shapes(ActiveChart.Parent.Name).Line.Visible = msoFalse