Create chart using VBA and remove border

dataryne picture dataryne · Aug 4, 2016 · Viewed 8.5k times · Source

I have a macro that creates a line graph for three data series. Excel automatically adds a border around the graph, and I despise this but cannot figure out how to remove it. I've tried variations on these:

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

This is a sample data set:

            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

And this is my current code:

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

Answer

cyboashu picture cyboashu · Aug 4, 2016

Change this line ActiveSheet.Shapes("Chart 1").Line.Visible = msoFalse with

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