簡體   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