使用VBA在Excel图表中制作轴标题

编程入门 行业动态 更新时间:2024-10-28 12:29:06
本文介绍了使用VBA在Excel图表中制作轴标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用VBA创建了一个excel图表,然后格式化轴标题和字体的大小。以下代码适用于水平轴

I create an excel chart with VBA and then format the axis titles and fonts' size. The following code works well for the Horizontal axis

cht.SetElement msoElementPrimaryCategoryAxisTitleAdjacentToAxis cht.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Factor of Safety" cht.Axes(xlCategory, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15

但是,垂直轴的类似代码

However, the similar code for the Vertical axis

cht.SetElement msoElementPrimaryValueAxisTitleAdjacentToAxis cht.Axes(xlValue, xlPrimary).AxisTitle.Text = "Depth [mCD]" cht.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15

导致错误424需要对象。您能告诉我发生了什么事吗?

leads to the error 424 "Object required". Could you please tell me what happened ?

推荐答案

在最新版本的Excel中,您可以使用 SetElement 和一个命名常量,以向图表添加要素。 似乎容易一些,但实际上却不那么直观,而且可能不可靠。

In recent versions of Excel, you can use SetElement with a named constant to add features to the chart. This seems easier, but it's less intuitive in terms of what it actually does, and it can be unreliable.

因此,代替这个:

cht.SetElement msoElementPrimaryValueAxisTitleAdjacentToAxis cht.Axes(xlValue, xlPrimary).AxisTitle.Text = "Depth [mCD]" cht.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15

执行以下操作:

cht.Axes(xlValue, xlPrimary).HasTitle = True cht.Axes(xlValue, xlPrimary).AxisTitle.Text = "Depth [mCD]" cht.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 15

更多推荐

使用VBA在Excel图表中制作轴标题

本文发布于:2023-07-20 05:02:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1164456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图表   标题   VBA   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!