I am writing some VBA code intended to be compatible with Excel 2007 and newer versions. Since starting from Excel 2013 the Chart Series Filtering option and the related Chart.FullSeriesCollection
object were introduced, I have included in my code an If
statement to choose either this object or the older .SeriesCollection
one depending on the Excel version.
However, VBA can't compile the code in Excel 2007 since .FullSeriesCollection
is not defined. I wanted to try Late Binding so that the compiler skips the If
statement that includes that undefined object, but Excel 2007 (with VBA version 6.3) doesn't recognize the Option Strict Off
line either; I can only choose from Base
, Compare
, Explicit
or Private
to follow the Option
statement.
How can I get the older VBA compiler to skip the line where .FullSeriesCollection
is used? I have been learning VBA for 3 days, so excuse me if this is super obvious.
The relevant part of my code:
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]