使用VBA更改图表字体 [英] Change chart font using VBA

查看:605
本文介绍了使用VBA更改图表字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用VBA更改Excel图表的字体?

How do I change the font of an Excel chart using VBA?

如果我手动选择图表,并在手动更改字体名称和大小的同时记录宏,则会在下面看到宏.但是当我立即重播该宏时,它将引发运行时错误:指定的值超出范围."因此,看起来宏记录器有错误.这意味着我无法自己找出更改字体的代码.

If I manually select the chart, and record a macro while I manually change the font name and size, I get the macro below. But when I immediately replay the macro, it throws a run-time error: "The specified value is out of range." So it looks like the macro recorder has a bug. Which means I can't figure out the code to change the font myself.

Sub Macro6()
'
' Macro6 Macro
'

'
    With ActiveSheet.Shapes("Chart 1").TextFrame2.TextRange.Font
        .NameComplexScript = "Verdana"
        .NameFarEast = "Verdana"
        .Name = "Verdana"
    End With
    ActiveSheet.Shapes("Chart 1").TextFrame2.TextRange.Font.Size = 14
End Sub

我知道作为一种选择,我可以一次更改每个元素的字体(标题,轴标题,轴等),但这很繁琐,并且遗忘了一些元素(系列)的可能性.点标签,趋势线方程式...).

I know that as an alternative, I could change the font of each individual element one at a time (title, axis titles, axes, ...) but this is tedious and leaves open the possibility of forgetting some elements (series point labels, trendline equations, ...).

我希望更改图表的默认"字体,以使其所有元素都具有该字体.

I'm looking to change the "default" font of the chart, such that all its elements will have that font.

推荐答案

实际上是一个奇怪的错误...做了同样的事情,但是我进入了对象浏览器(F2),目的是使用Chart而不是Shape.

Indeed that a strange error... Did the same, but I went to the Object Browser (F2) with the objective to work around with Chart and not Shape.

经过一番尝试,我让这个开始工作了:

After some trying, I got this one to work :

With ActiveSheet.ChartObjects("Graph").Chart.ChartArea.Format.TextFrame2.TextRange.Font
    .Name = "Verdana"
    .Size = 14
End With

这很简单 ,我尝试了一些更奇怪的事情(因为TextRange2类中有一个.Count属性)

It's pretty simple, I tried more curious things (as there is a .Count property in TextRange2 class)

它工作正常,并且更改了整个图表的字体
只需要知道图形的名称即可.

It is working just fine and does change the font of the entire chart,
you just have to know the name of your graph.

或者,确保选择了图表,并使用ActiveChart而不是ActiveSheet.ChartObjects("Graph").Chart.

Alternatively, make sure the chart is selected, and use ActiveChart instead of ActiveSheet.ChartObjects("Graph").Chart.

这篇关于使用VBA更改图表字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆