Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 231963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:58:38+00:00 2026-05-11T19:58:38+00:00

I have code that needs to run on both Excel 2003 and Excel 2007,

  • 0

I have code that needs to run on both Excel 2003 and Excel 2007, and there are a few spots where changes in the versions cause the code to halt. I tried separating these lines out with If-Else statements, but the code won’t compile on either because it doesn’t recognize the code used for the other. Is there any way I could tell one version to ignore a block of code, similar to a C or C++-style #ifdef, in VBA?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:58:38+00:00Added an answer on May 11, 2026 at 7:58 pm

    This is a good starting point, but it won’t work with the version of Excel that its running on, since that can only be figured out at run-time, not compile time.

    If you need to branch your code based on information only discoverable at run time you might consider late binding as a solution. There are two ways you can sneak around version problems.

    The first way can be used if you need to Access a property or method that only exists in certain versions, you can use CallByName. The advantage of call by name is that it allows you to preserve early binding (and intellisense) for your objects as much as possible.

    To give an example, Excel 2007 has a new TintAndShade property. If you wanted to change the color of a range, and for Excel 2007 also ensure TintAndShade was set to 0 you would run into trouble because your code won’t compile in Excel 2003 which does not have TintAndShade as a property of the range object. If you access the property that you know is not in all versions using CallByName, you code will compile in all versions fine, but only run in the versions you specify. See below:

    Sub Test() 
        ColorRange Selection, Excel.Application.version, 6 
    End Sub 
    Sub ColorRange(rng As Excel.Range, version As Double, ParamArray args() As Variant) 
        With rng.Interior 
            .colorIndex = 6 
            .Pattern = xlSolid 
            If version >= 12# Then 
                 'Because the property name is stored in a string this will still compile.
                 'And it will only get called if the correct version is in use.
                CallByName rng.Interior, "TintAndShade", VbLet, 0 
            End If 
        End With 
    End Sub 
    

    The second way is for classes that have to be instantiated via “New” and don’t even exist in old versions. You won’t run into this problem with Excel, but I will give a quickie demo so you can see what I mean:

    Imagine that you wanted to do File IO, and for some bizarre reason not all of the computers had the Microsoft Scripting Runtime on them. But for some equally bizarre reason you wanted to make sure it was used whenever it was available. If set a reference to it and use early binding in your code, the code won’t compile on systems that don’t have the file. So you use late binding instead:

    Public Sub test()
        Dim strMyString As String
        Dim strMyPath As String
        strMyPath = "C:\Test\Junk.txt"
        strMyString = "Foo"
        If LenB(Dir("C:\Windows\System32\scrrun.dll")) Then
            WriteString strMyPath, strMyString
        Else
            WriteStringNative strMyPath, strMyString
        End If
    End Sub
    
    Public Sub WriteString(ByVal path As String, ByVal value As String)
        Dim fso As Object '<-Use generic object
        'This is late binding:
        Set fso = CreateObject("Scripting.FileSystemObject")
        fso.CreateTextFile(path, True, False).Write value
    End Sub
    
    Public Sub WriteStringNative(ByVal path As String, ByVal value As String)
        Dim lngFileNum As Long
        lngFileNum = FreeFile
        If LenB(Dir(path)) Then Kill path
        Open path For Binary Access Write Lock Read Write As #lngFileNum
        Put #lngFileNum, , value
        Close #lngFileNum
    End Sub
    

    There is a comprehensive list of all Adds and Changes to Excel Object Model since 2003:
    http://msdn.microsoft.com/en-us/library/bb149069.aspx
    For changes between 1997 and 2000 go here:
    http://msdn.microsoft.com/en-us/library/aa140068(office.10).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're looking to determine the integer log base 2 of… May 14, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer it seems like this was one plugin error or something… May 14, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer There are only a few reasons I know of why… May 14, 2026 at 4:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.