Consulting

Results 1 to 6 of 6

Thread: Solved: Compiler Directives (#Const, #If, etc.)

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location

    Solved: Compiler Directives (#Const, #If, etc.)

    Recently I had to make a WB 2003 and 2007 compatible.

    To make my life easy and only have one version to maintain, I bracketed the 2007 code (Ribbon, etc.) and the 2003 code (Commandbars, etc.) with #If / #Else / #EndIf directives

    [vba]
    Option Explicit

    #Const Ver = "2007"

    Sub DoStuff()
    #If Ver = "2007" Then
    Call Do2007

    #Else
    Call Do2003

    #End If

    End Sub
    [/vba]

    1. The #Const is private to each module. Is there any way to avoid that?

    2. I find it hard to believe that there aren't any built in directives, such as OS, Platform, Version, etc. Are there some and I'm missing them?

    Thanks

    Paul

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]MsgBox Application.Version
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Thanks, not too sure how that will help.

    Some 2003 VBA statements will not 'compile' at all in 2007, and vice versa.

    So I need to use conditional compiler directives to allow some 2003 VBA to be 'bypassed' if the WB is used in Excel2007, and the same going the other way

    Paul

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    In the project properties in the VBIDE for your project, in the 2007 version enter

    Ver2007=-1

    in the Conditional Compilation Arguments and use, nothing for the 2003 verison

    [vba]

    Sub DoStuff()
    #If Ver2007 Then
    Call Do2007

    #Else
    Call Do2003

    #End If

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Works great

    Still seems a little funny that MS doesn't have a least a few standard Directives pre-defined

    Thanks

    Paul

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    They do have a few, But not that one. Don't forget, VBA has been aboandoned as regards enhancements.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •