XL: Securing Visual Basic Code in Microsoft Excel

Last reviewed: December 23, 1997
Article ID: Q167909
The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows, version 5.0
  • Microsoft Excel for Windows 95, version 7.0

SUMMARY

In Microsoft Excel 97, unlike earlier versions of Microsoft Excel, you cannot hide Visual Basic for Applications modules. If you want to prevent users from seeing Visual Basic code, you can lock the Visual Basic project in the workbook and prevent users from viewing it. However, if you save the workbook in either the Microsoft Excel 5.0/95 or the Microsoft Excel 97 & 5.0/95 file format, the module protection is lost when you open the workbook in Microsoft Excel 5.0 or 7.0. Therefore, to use this method, you must maintain separate versions of the workbook for users of Microsoft Excel 97 and users of Microsoft Excel 5.0 and 7.0.

This article describes a method of protecting Visual Basic code while allowing users of different versions of Microsoft Excel to use the same version of your application. This method requires that you create the application in Microsoft Excel 5.0 or 7.0 and then save it as an add-in file. This method hides the code in all versions of Microsoft Excel. Any add-in code you create in Microsoft Excel 5.0 or 7.0 is not visible when the file is opened in Microsoft Excel 97.

MORE INFORMATION

In the example in this article, you compile an application into an add-in file. The interface for the application consists of two worksheets, which are copied into a new workbook when the add-in file is opened.

NOTE: To create an add-in that is compatible with Microsoft Excel 5.0 and 7.0 and Microsoft Excel 97, use the earliest version of Microsoft Excel in which the add-in will be used. For example, if you use Microsoft Excel 7.0 and Microsoft Excel 97, create the add-in in Microsoft Excel 7.0.

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Creating the Source Workbook File for the Add-In

To create the workbook, do the following:

  1. Close and save any open workbooks, and then create a new workbook.

  2. If the new workbook does not contain at least two worksheets, insert a new worksheet. To do this, click Worksheet on the Insert menu.

  3. If the sheet tabs are not visible, click Options on the Tools menu, and then click the View tab. Click Sheet Tabs, and then click OK.

  4. Double-click the Sheet1 tab. In the Rename Sheet dialog box, type "AddinSheet1" (without the quotation marks) in the Name Box, and then click OK.

  5. Double-click the Sheet2 tab. In the Rename Sheet dialog box, type "AddinSheet2" (without the quotation marks) into the Name Box, and then click OK.

  6. On the Insert menu, point to Macro, and then click Module to insert a Visual Basic module sheet into the workbook.

  7. Type the following code into the module sheet:

    ' This subroutine copies the worksheets to use as the interface ' for the application into a new workbook each time that the ' add-in is opened.

           Sub Auto_Open()
    
               Dim NewBook As Workbook
               Dim Ctr As Integer
    
               Application.ScreenUpdating = False
               Application.DisplayAlerts = False
    
               ' Create a new workbook.
               Set NewBook = Workbooks.Add
    
               ' Copy the two worksheets into the new workbook.
               ThisWorkbook.Sheets(Array("AddinSheet1", "AddinSheet2")).Copy _
               before:=NewBook.Sheets(1)
    
               ' Delete all of the other sheets in the new workbook. The
               ' initial value of the counter is 1 greater than the number of
               ' worksheets that you want to copy into the new workbook.
               For Ctr = 3 To NewBook.Sheets.Count
    
                  NewBook.Sheets(3).Delete
    
               Next
    
           End Sub
    
           ' This sample demonstrates that any buttons that you place on the
           ' interface worksheets are still functional when the worksheets are
           ' copied into a new workbook.
           Sub Test()
    
               MsgBox "This is a test"
    
           End Sub
    
    

  8. Click AddinSheet1.

  9. On the View menu, click Toolbars. In the Toolbars list, click Forms, and then click OK.

  10. Draw a button on the worksheet using the Create Button tool.

  11. In the Assign Macro dialog box, click Test in the list of available macros, and then click OK.

  12. On the file Menu, click save. In the File Name box, type "Test.xls" (without the quotation marks), and then click Save.

Creating the Add-In File

To create the add-in file, do the following:

  1. Switch to Module1.

  2. On the Tools menu, click Make Add-In. In the File Name box, type "Test.xla" (without the quotation marks), and then click Save.

  3. Close Test.xls.

You should have two files, Test.xls and Test.xla. The add-in file, Test.xla, is the file that you must distribute to your users. Microsoft recommends that you keep a copy of Test.xls because you will need it to update the add-in.

When you open Test.xla, a new workbook is created with two worksheets that are exact copies of the worksheets in the add-in file. If you click the button on AddinSheet1, the Test subroutine in the add-in file runs.

REFERENCES

"Visual Basic User's Guide," version 5.0, Chapter 13, "Converting a Workbook to an Add-in Application"

For more information about creating add-ins in Microsoft Excel 7.0, click the Index tab in Microsoft Excel Help, type the following text

   add-ins, creating in Visual Basic

and then double-click the selected text to go to the "What is an add-in application, and where can I learn more about add-ins?" topic.


Additional query words: XL97 secure securing protect
Keywords : xladdins xlvbahowto xlvbainfo kbmacro kbprg kbui
Version : 5.0 7.0 97
Platform : WINDOWS
Issue type : kbhowto


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: December 23, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.