繁体   English   中英

具有不同Excel版本的Excel VBA组合图(2010年与2016年)

[英]Excel VBA Combo Chart with different Excel versions (2010 vs 2016)

我有一个宏,在Office 2010和Office 2016之间似乎有不同的行为,特别是“组合图”功能。 (我们正在Office 2010(Win7)和Office 2016(Win10)之间过渡)。

我想弄清楚的是,如果用户在2010年或2016年打开宏(如果可能的话)并执行“创建图表”子程序,那么如何在宏中开发一个函数以“感知”

当我最初在2016年开发宏时,这是创建组合图的子项。

'' Create ComboChart (Excel2016only)
'
    Range("A1:C11").Select
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("$A$1:$C$11")
    ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
    ActiveChart.FullSeriesCollection(1).AxisGroup = 1
    ActiveChart.FullSeriesCollection(2).ChartType = xlLine
    ActiveChart.FullSeriesCollection(2).AxisGroup = 1
    ActiveChart.FullSeriesCollection(2).ChartType = xlLineMarkersStacked
    ActiveChart.FullSeriesCollection(2).AxisGroup = 2

当2010用户尝试运行此程序时,它将在.FullSeriesCollection(1).ChartType = xlColumnClustered中“编译错误:找不到方法或数据成员”。 我相信这是因为组合图表直到2016年才出现。

因此,我不得不手动记录宏步骤以弄清楚如何在2010年做辅助轴,因为它不是本机的。

' ComboChartExcel2010 Macro
'
    Range("A1:C11").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$C$11")
    Application.WindowState = xlMaximized
    ActiveChart.Legend.Select
    ActiveChart.Legend.LegendEntries(2).Select
    ActiveChart.SeriesCollection(2).Select
    ActiveChart.SeriesCollection(2).AxisGroup = 2
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.SeriesCollection(2).Select
    ActiveChart.SeriesCollection(2).ChartType = xlLineMarkers

有什么建议可以开发一个例程,使其可以根据用户正在运行的Excel版本运行任一子程序? 现在,我必须有一个UI框,要求用户选择他们正在运行的那个。

选中Application.Version会告诉您您正在/正在使用的Excel版本。

IF Application.Version = 15 Then ...您正在使用Excel 2013

12 = Excel 2007

14 = Excel 2010

15 = Excel 2013

16 = Excel 2016

在Excel 2010中,这做得很好。

ActiveSheet.Shapes.AddChart(xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("$A$1:$C$11")
ActiveChart.SeriesCollection(1).ChartType = xlColumnClustered
ActiveChart.SeriesCollection(1).AxisGroup = 1
ActiveChart.SeriesCollection(2).ChartType = xlLine
ActiveChart.SeriesCollection(2).AxisGroup = 1
ActiveChart.SeriesCollection(2).ChartType = xlLineMarkersStacked
ActiveChart.SeriesCollection(2).AxisGroup = 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM