使用VBA将图表系列放在辅助X轴上 [英] Using VBA to put a chart series on the secondary X axis

查看:374
本文介绍了使用VBA将图表系列放在辅助X轴上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个折线图,我试图通过VBA添加一个给定Y值的水平线。我正在关注本指南,该指南建议在现有图表中添加一行,然后更改其分散类型,使Excel将自动将该系列放在辅助X轴上。然后可以进一步格式化。



我的问题是,当使用VBA进行此操作时,我不明白如何将一条线切换到辅助X轴上。默认情况下,所有新行将在主X轴上。



例如,假设 chrt 是一个Chart对象:

 '启用辅助X轴(我想)
chrt.HasAxis(XlAxisType.xlCategory,XlAxisGroup.xlSecondary) = True

Dim se As Series
Set se = chrt.SeriesCollection.NewSeries
se.ChartType = xlXYScatterLinesNoMarkers

'在Y = 0
se.xValues = Array(0,1)
se.values = Array(0,0)

现在,如何在辅助X轴上获取 se

解决方案

我按照这个链接Excel 2003 ,并记录了一个宏。请注意,对于Excel 2007图表,有这样一个不幸的情况与记录宏。但是,您再次在Excel 2010中幸运。所以要注意你的版本,如果你要做一个宏,然后复制代码...



根据你的更改工作表和图表名称。

  With Sheets(3).ChartObjects(Chart 1)
.SeriesCollection(2).AxisGroup = xlSecondary
.HasAxis(xlCategory,xlPrimary)= True
.HasAxis(xlCategory,xlSecondary)= True
.HasAxis(xlValue,xlPrimary)= True
.HasAxis(xlValue,xlSecondary) = True
.Axes(xlCategory,xlPrimary).CategoryType = xlAutomatic
.Axes(xlCategory,xlSecondary).CategoryType = xlAutomatic
End with
/ pre>

I have a line chart to which I'm attempting to add, via VBA, a horizontal line at a given Y value. I'm following this guide, which suggests adding a line to an existing chart and then changing its type to scatter, which causes Excel to put the series on the secondary X axis automatically. It can then be formatted further.

My problem is that when doing this with VBA I don't understand how to switch a line onto the secondary X axis. By default all new lines will go on the primary X axis.

For example, suppose chrt is a Chart object:

' Enable secondary X axis (I think)
chrt.HasAxis(XlAxisType.xlCategory, XlAxisGroup.xlSecondary) = True

Dim se As Series
Set se = chrt.SeriesCollection.NewSeries
se.ChartType = xlXYScatterLinesNoMarkers

' Add a line at Y = 0
se.xValues = Array(0, 1)
se.values = Array(0, 0)

Now, how to get se onto the secondary X axis?

解决方案

I followed this link for Excel 2003 and recorded a macro. Please note that for Excel 2007 charts have such a unfortunate cases with recording macros. But you get lucky in Excel 2010 again. So watch out for your version if you are going to do it based on a macro and then copy the code...

Change sheet and chart name according to yours.

With Sheets(3).ChartObjects("Chart 1")
     .SeriesCollection(2).AxisGroup = xlSecondary
     .HasAxis(xlCategory, xlPrimary) = True
     .HasAxis(xlCategory, xlSecondary) = True
     .HasAxis(xlValue, xlPrimary) = True
     .HasAxis(xlValue, xlSecondary) = True
     .Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
     .Axes(xlCategory, xlSecondary).CategoryType = xlAutomatic
  End With

这篇关于使用VBA将图表系列放在辅助X轴上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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