Search code examples
excelvbaexcel-charts

Changing Selected Chart's Border Color


If I record an Excel macro to change the border color of a chart, then VBA returns

Sub change_bordercolor()
    With ActiveSheet.Shapes("Chart 1").Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 0, 0)
        .Transparency = 0
    End With
End Sub

so I wanted to code to change the border color of not the "Chart 1" but a currently selected chart as follows.

Sub change_bordercolor()
    Selection.ShapeRange.Line.ForeColor.RGB = RGB(0, 0, 0)
End Sub

but this returns the 438 runtime error. Where do I need to change to use the Selection instead of the "Chart 1?"


Solution

  • To change the bordercolor of the Active Chart you must have selected you can use this code:

    Sub Macro1()
    
    If Application.ActiveChart Is Nothing Then Exit Sub
    
    
    With ActiveSheet.Shapes(Replace(Application.ActiveChart.Name, ActiveSheet.Name & " ", "")).Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 255, 0)
        .Transparency = 0
    End With
    
    End Sub
    

    I've set it up to change border color to green, but you can adapt it to your needs. This is how it works:

    enter image description here

    It changes border color of active selected chart.