本文介绍了使用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图表中制作轴标题
发布评论