Search code examples
excelvbaloopsformatexcel-charts

unable to format charts in vba


I have 18 charts and want to change their formatting, but the following code doesnt work

Dim ws As Worksheet
Dim cht As Chart

Set ws = ActiveSheet

For Each cht In ws.ChartObjects

cht.Activate
  
cht.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Arial"
    
Next cht

by the nature of errors, it appears to be a syntax mistake but i could not figure it our despite reading through multiple queries


Solution

  • You need to loop the chart objects, then take the chart from those.

    Dim ws As Worksheet
    Dim cho As ChartObject
    Dim cht As Chart
    
    Set ws = ActiveSheet
    
    For Each cho In ws.ChartObjects
    
    Set cht = cho.Chart
    
    cht.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Times New Roman"
    
    Next cho