excelvbagraphcharts

How to change Chart xAxes font color from black to red via vba code?


How to change Chart xAxes font color from black to red via vba code?

The following picture shows my question.

picture

I have tried the following code without success.

Public Sub Macro1()

'Delete all charts
For i = ActiveSheet.Shapes.Count To 1 Step -1
    If ActiveSheet.Shapes(i).Type = msoChart Then
        ActiveSheet.Shapes(i).Delete
    End If
Next i

'Add a chart.
With ActiveSheet.ChartObjects.Add(Left:=10, Top:=10, Width:=600, Height:=400)
    .Chart.ChartArea.Format.TextFrame2.TextRange.Font.Size = 20
End With

'Add a serie.
With ActiveSheet.ChartObjects(1).Chart.SeriesCollection.NewSeries
    .ChartType = xlLine
    .XValues = Array(1, 2, 3, 4, 5)
    .Values = Array(150000, 20000, 80000, 120000)
End With

'Axes(xlValue) settings.
With ActiveSheet.ChartObjects(1).Chart.Axes(xlValue)
    'The following line throws an error. How to solve this error?
    '.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = vbRed
End With

End Sub

Solution

  • For example:

    Dim cht As Chart
    Set cht = ActiveSheet.ChartObjects(1).Chart
    cht.Axes(xlValue).TickLabels.Font.Color = vbRed