无法格式化 vba 中的图表

unable to format charts in vba

我有 18 个图表,想更改它们的格式,但以下代码不起作用

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

根据错误的性质,这似乎是一个语法错误,但尽管阅读了多个查询我还是无法理解

您需要循环图表对象,然后从中获取图表。

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