XL97: Conditional Compilation Code Lost With 5.0/95 Format

Last reviewed: March 13, 1998
Article ID: Q161959
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SYMPTOMS

If you save a Microsoft Excel 97 workbook in the Microsoft Excel 5.0/95 Workbook file format, you lose any Conditional Compilation directives in your code.

NOTE: You are not warned about the loss of your code when saving your file in the 5.0/95 file format.

CAUSE

Conditional Compilation directives are not used in earlier versions of Visual Basic for Applications and Microsoft Excel.

RESOLUTION

If you have already saved your file in the 5.0/95 file format, you must reenter your Conditional Compilation directives when you open your file in Microsoft Excel 97.

If you have not yet saved your file in the 5.0/95 file format and you want to preserve your Conditional Compilation directives, either save your module as a .bas file (you can import it into other projects) or comment the Conditional Compilation directives, and then save your file in the 5.0/95 file format.

MORE INFORMATION

You can use conditional compilation, using #If ... Then ... #Else directives, to run blocks of code selectively. This is typically used in cross-platform sub procedures. The #If ... Then ... #Else directives evaluate expressions containing conditional compiler constants to determine what code to execute in the macro.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

The following macro provides an example of using conditional compilation:

  1. Save and close any open workbooks and then open a new workbook.

  2. Start the Visual Basic Editor (press ALT+F11).

  3. On the Insert menu click Module.

  4. In the module, type the following code:

          #Const test = 1
    

          Sub Cond_Comp()
    
              #If test Then
                  MsgBox "test evaluates to True"
    
              #Else
                  MsgBox "test evaluates to False"
    
              #End If
    
          End Sub
    
    

  5. Run the Cond_Comp macro.

Since the conditional compilation constant "test" has a value of 1, a message box will display the message "test evaluates to True".

  1. Change the first line of code in the module to:

          #Const test = 0
    

  2. Run the Cond_Comp macro.

    A message box with "test evaluates to False" will be displayed.

Conditional compilation constants can also be specified in the Project Properties dialog box instead of in a module. To utilize this technique in the previous example, follow these steps:

  1. Comment the first line in the module so that it looks like the following:

          '#Const test = 0
    

  2. On the Tools menu, click VBAProject Properties and click the General tab.

  3. In the Conditional Compilation Arguments box, type the following:

          test = 1
    

  4. Click OK.

  5. Run the Cond_Comp macro.

Since the conditional compilation constant "test" has a value of 1, a message box displays the message "test evaluates to True."

NOTE: If you want to specify more than one conditional compilation constant in the Conditional Compilation Arguments box, use a colon (:) as a separator. For example, you would type the following into the Conditional Compilation Arguments box:

   first = 1 : second = 0

REFERENCES

For more information about Conditional Compilation, click the Index tab in Visual Basic for Applications Help, type the following text

   conditional compiling

and then double-click the selected text to go to the "Understanding Conditional Compilation" topic.


Additional query words: 97 XL97
Keywords : kbcode kbprg xlvbahowto xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.