Results 1 to 3 of 3
  1. #1
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    San Jose, California, USA
    Thanked 0 Times in 0 Posts

    Using Conditional Compile (Excel 2000)

    Hi all,
    I need insight into how to use the conditional compile feature in the VBA IDE. I've used this sort of thing before in C/C++ but can't quite get this one to fly. <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15> I want to add some debug code that only gets run if my debug variable is set. I tried it two ways:

    1. Defining #Const InDebug=1
    2. Entering InDebug-=1 in the 'conditional compilation arguments' field under project properties.

    I then have code that looks something like:
    <pre>#if InDebug then
    msgbox "Debug is On"
    msgbox "Debug is Off"
    #end if

    This work is I define InDebug as option #1 above but can't get it to work with option #2. The help wasn't too helpful and of the three great VBA books I have only one even mentions it. Help seems to say that #Const is at the module level and I can't make a global public one, true? What's the point then? <img src=/S/bash.gif border=0 alt=bash width=35 height=39>

    Also, can I programatically set InDebug =0 (like if I wanted the end user to turn it on/off w/o me having to edit the code and give them a new version).

    The point of all this is that I'd like to offer the user a means to enable/disable debug mode on the fly (otherwise it's not much good if I have to edit code to do it for them).

    Any clues? <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Thanked 28 Times in 28 Posts

    Re: Using Conditional Compile (Excel 2000)

    You write
    <hr>can I programatically set InDebug =0<hr>
    No, that is impossible - it contradicts the principle of conditional compiling. Conditional compiling means that the .exe file created by the compiler will be different if you change the value of the conditional compilation constant. Only specific chunks of code, depending on the value of the conditional compiling constant, will be present in the .exe file. Chunks of code not meeting the criteria simply don't exist in the .exe file! So you can't switch in runtime - there is nothing to switch to.

    If you want to provide the user with two different modes of operation, use a global variable to keep track of the mode.

  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    South Carolina, USA
    Thanked 0 Times in 0 Posts

    Re: Using Conditional Compile (Excel 2000)

    Your second method does not work because when the code is compiled, the statement InDebug has not yet been run since it can't be run until after it is compiled. Therefore, when the conditional statements are compiled, InDebug has not been set. Const statements are done at compile time, therefore the first one works.

    You can't change the conditional compile condition at run time because, as a result of the above, the condition must be a Const. In addition, changing the variable at run time is after the compile is complete, therefore the conditional compile statements are already compiled.
    Legare Coleman

Posting Permissions

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