Limiting Where a Workbook is Used

Written by Allen Wyatt (last updated June 5, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


Shay works with a volunteer organization and they have developed an Excel workbook specifically for their volunteers. They don't want it copied or shared with outside individuals, so Shay wonders if there is a way to protect the workbook so that it will work only for a specific computer and no others.

There are ways to try to enforce security of workbooks, but there are considerations to take into account before settling on the best way. Understand that just about any approach is going to inconvenience users to one degree or another. Also, the approach you take may depend on whether the users are solely on your internal network or they are volunteers not connected to your network.

An easy way to start is to simply password-protect the workbook and then let your volunteers know the password. The two-fold drawback to this is that any password you pass on to your volunteers would continue to work after they were no longer volunteers and for anyone to whom the volunteers might pass the workbook. This is true even if you do an individual password for each volunteer.

Any other approach would rely on the addition of macros to the workbook. A great way is for the macro to check the name of the computer on which the workbook is being opened. This is an example of such a macro:

Private Sub Workbook_Open()
    Dim sComputerName As String
    Dim sPossible As String
    Dim sTemp As String

    sPossible = "[NEWDELL][Computer1][Order Entry][Dan's System]"
    sPossible = sPossible & "[Computer2][Computer3]"

    sComputerName = Environ("computername")
    sTemp = "[" & sComputerName & "]"

    If InStr(sPossible, sTemp) Then
        MsgBox "Welcome to the workbook."
    Else
        MsgBox "You are not authorized to open this workbook."
        Workbooks(ActiveWorkbook.Name).Close SaveChanges:=False
    End If
End Sub

The idea behind the macro is that you would define, in the sPossible string, the names of each computer on which it is permissible to open the workbook. All you need to do is make sure you get the machine names spelled exactly correctly and enclose them within square brackets in the sPossible string. The Environ function returns the actual computer name, it is placed in the sTemp variable within the brackets, and then the InStr function is used to see if it is in the list of possible machines. If it is, then a message box is displayed; if it isn't, then a message box is displayed, and the workbook is closed without saving any changes.

If you prefer something even a bit more secure, you could also use the Environ function to return the "username" value, which would give you the username for the account being used on the computer.

This macro approach is relatively simple, but it does require knowing computer names (and/or usernames) before it will work properly. It also assumes that the user doesn't know enough about Excel to bypass macros by holding down the Shift key as the program starts. If you suspect the user may know that much, you'll need to take more steps, such as the following:

  • Make sure that a single worksheet is visible in the workbook. All other worksheets should be "very hidden" so the user cannot display them using normal steps.
  • If the macro determines the user is allowed, hide the single visible worksheet and make all the "very hidden" worksheets visible.
  • When the workbook is closed, reverse the process and hide all the worksheets (set the Visible property of each of them to xlSheetVeryHidden) and make the single worksheet visible again.

Using this approach makes sure that even if the user disables macros (by holding down the Shift key during startup), he or she cannot get to the worksheets because a macro is required to make them visible.

You could utilize a variation on the above approach so that instead of relying on the Environ function you could have the macro check for the presence of a predetermined file in a particular location on the system. This means, of course, that you need a way to get your hands on the system ahead of time to put the proper file in the proper location.

Finally, whatever macro-based approach you choose, you should ensure that the VBA project for the workbook is protected with a password. This will stop someone from going and looking at your macro code to figure out what is going on with the protection.

There is something else you always need to keep in mind when trying to secure your workbooks: Whatever safeguards you put in place can always be circumvented with enough knowledge and patience. If your users are not that technically sophisticated, then you should have no problems. If they know a lot about Excel and macros, however, then all bets are off.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13666) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Creating Categories for Your Table of Authorities

A table of authorities is normally divided into separate sections based on categories you define. Here's how to create ...

Discover More

Sorting an Entire List

Need to sort all the data in a table? Here's the fastest and easiest way to do it.

Discover More

Getting Word to Remember the Default Date and Time Format

One way to insert the current date into your document is to use the Date and Time dialog box. The Default button in the ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Forcing a Workbook to Close after Inactivity

Tired of your workbooks being left open on the screen where they can be seen by anyone passing by? Here's a way to have ...

Discover More

Unresponsive Excel when Double-Clicking a Workbook

There are two ways you can load a workbook in Excel, and each approach should result in the workbook being loaded ...

Discover More

Reducing File Size

As you work with a workbook (particularly one that contains macros) you may notice that the workbook size can become ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four less than 6?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.