【发布时间】:2017-05-12 19:08:37
【问题描述】:
我正在编写一些旨在与 Excel 2007 和更新版本兼容的 VBA 代码。从 Excel 2013 开始,引入了图表系列过滤选项和相关的 Chart.FullSeriesCollection 对象,我在我的代码中包含了一个 If 语句来选择这个对象或旧的 .SeriesCollection 一个,具体取决于 Excel 版本。
但是,由于未定义 .FullSeriesCollection,VBA 无法编译 Excel 2007 中的代码。我想尝试后期绑定,以便编译器跳过包含该未定义对象的 If 语句,但 Excel 2007(使用 VBA 版本 6.3)也无法识别 Option Strict Off 行;我只能从Base、Compare、Explicit 或Private 中选择遵循Option 声明。
如何让旧版 VBA 编译器跳过使用 .FullSeriesCollection 的行?我已经学习 VBA 3 天了,如果这很明显,请见谅。
我的代码的相关部分:
Private Sub EventChart_MouseDown(ByVal Button As Long, _
ByVal Shift As Long, _
ByVal x As Long, _
ByVal y As Long)
Dim ElementID As Long, Arg1 As Long, Arg2 As Long, Arg1b As Long
Dim myX As Variant, myY As Double
Dim xlVrsion As Integer, verSerColl As Object
xlVrsion = CInt(Left(Application.Version, 2)) 'Get Excel version and convert to an integer (2007 = 13.0; 2010 = 14.0; 2013 = 15.0; 2016 = 16.0)
With ActiveChart
.GetChartElement x, y, ElementID, Arg1, Arg2
If ElementID = xlSeries Then
If xlVrsion >= 15 Then 'Check if Excel version is equal or newer than 2013.
Set verSerColl = .FullSeriesCollection(Arg1)
Else
Set verSerColl = .SeriesCollection(Arg1)
End If
'[More irrelevant code]
【问题讨论】: