Excel 2010 coding in Excel 2007

User avatar
ErikJan
BronzeLounger
Posts: 1246
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Excel 2010 coding in Excel 2007

Post by ErikJan »

I use Excel 2010 and have introduced some Sparkline charts in my sheet. I know that when my sheet is opened in Excel 2007 (others still use that version), there should be no errors but sparlines are simply not shown (please correct me if I'm wrong, I still need to test butthis is what I believe now). So there are no problems here.

However, my tool loads more adat and in VBA code, I update the range of the SparkLine group.... Now I can imagine thiat this WILL generate an error message as VBA in 2007 does not know these new objects, methods and properties. So how do I work around this (in VBA, I know that in this case I might get away with a dynamic range maybe but I'd like to learn the trick in VBA).

My guess would be that I should use compiler directives #IF #THEN #ELSE #ENDIF. Would this work?

ExcelVersion=Application.Version
#IF ExcelVersion>=14 then
<mycode>
#ENDIF

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Excel 2010 coding in Excel 2007

Post by HansV »

You can use the predefined compiler constant VBA7. This is undefined in Excel 2007 or earlier, and True in Excel 2010 or later. So:

#If VBA7 Then
<yourcode>
#End If
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1246
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Excel 2010 coding in Excel 2007

Post by ErikJan »

Works! Thank you