Restrict Workbook to Specific Users Only in Excel VBA - Owlcation - Education
Updated date:

Restrict Workbook to Specific Users Only in Excel VBA

Author:

Quick Overview

Excel is a commonly used application for sharing information around the workplace, with networked storage almost being a certain in all workplaces, some information may need to be kept from wondering eyes. Using the below guide, you'll be able to create a workbook that will automatically check the windows user that is logged in and allow / disallow access to the workbook.

This has been tested on Excel 2014 and later and Windows 10. Previous versions should work, but may not.

excel-vba-restrict-workbook-to-specific-users-only

Step 1 - Accessing VBA And Opening The Workbook Module

VBA can be accessed in one of two ways:

  1. Simply press ALT + F11
  2. Head to the Options and select "View Developer Tab" then click Visual Basic (2007 Onwards)

When the editor opens, you'll be presented with a grey window with a project manager down the left hand side.

Project Manager - This is where you move between your workbook sheets, forms and modules to view and edit code.

Double click on "ThisWorkbook", a window will open on the right hand side and you are now ready to add some VBA to the workbook

Step 2 - Configure Code To Run On Workbook Open

The code below will execute when the workbook is opened, providing Macros are enabled for the workbook

Private Sub Workbook_Open()

End  Sub

All of your code for this guide will be placed between these two lines. When the workbook is opened, the code between theses lines will be executed

Step 3 - Get The Currently Logged In User

Use the following code to obtain the current user that is logged in. Remember to place this code between the Private Sub and End Sub lines

Dim user As String

user = Application.UserName

Step 4 - Define The Users That Can Access The Workbook

Here's where you specify exactly which users can open the workbook. We'll use an Array here as it makes it particularly easy to loop through the array and check the names

Add the following code ABOVE "User = Application.Username"

Dim users(5) As String

users(0) = "SomeUser"
users(1) = "SomeUser"
users(2) = "SomeUser"
users(3) = "SomeUser"
users(4) = "SomeUser"

Replace "SomeUser" with usernames that are granted access to the workbook. You can add more users by simply changing the number in "Dim users(x)" and adding the new user to the end of the list.

Make sure you remember that the Dim users(x) declaration is the number of elements in the array, not the last number. It will always be +1 higher than the last element you index as indexing starts at 0

Step 5 - Loop Through Array And Test Access

Now we'll loop through the array that was just created and test each element to see if the user in the array matches the user that is logged in.

Use the following code

Dim access As Boolean
Dim i As Integer

access = False

For i = 0 To 4
    If users(i) = user Then
        access = True
        Exit For
    End If
Next

The above code first declares the new variables being used (access & i) and then sets access as false, the FOR statement then uses "i" to keep track of how many loops have been completed and loops through the users array by using users(i)

If the user in the array matches the user logged in (users(i) = user) then set access to TRUE and exit the for loop early.

If no user match is found, the access will still be set as false from before the loop was iterated.

Step 6 - Display A Message And Force Close The Workbook

If your user does not have access, we want to disallow them to proceed any further

If access = False Then
    MsgBox ("Sorry, the user """ & user & """ does not have the correct access rights to view this workbook")
    ActiveWorkbook.Close
End If
excel-vba-restrict-workbook-to-specific-users-only

The above image will be displayed if the user does not match any of the names in the array we made earlier

And that's it!

Full Code Example

Just want to grab the code and get it working? Here's the complete code:

Private Sub Workbook_Open()
Dim user As String
Dim users(5) As String

users(0) = "SomeUser"
users(1) = "SomeUser"
users(2) = "SomeUser"
users(3) = "SomeUser"
users(4) = "SomeUser"

user = Application.UserName

Dim access As Boolean
Dim i As Integer

access = False

For i = 0 To 4
    If users(i) = user Then
        access = True
        Exit For
    End If
Next

If access = False Then
    MsgBox ("Sorry, the user """ & "Liam" & """ does not have the correct access rights to view this workbook")
    ActiveWorkbook.Close
End If

End Sub

NOTE

Dont forget to change the number of loops if you change the number of users or some will get missed or an error will occur!

It's a good idea to also create a blank sheet to open the workbook onto, to stop details from being read whilst the message box is active.

Lastly, none of this will work if someone disables their macros!

Comments

Liam (author) from UK on June 24, 2020:

Yes, instead of performing your checks on workbook open, you could check the username when the sheet is activated. If the user is incorrect, send them back to a different sheet and display a message.

Alternatively, you could only allow the sheets of that user to be displayed when the workbook is first opened

Hafizian on June 24, 2020:

Is there a way one could restrict users to Sheet Tab ONLY? For example worksheet TEST has Three Tabs, Each tab is assigned to a user and I want to restrict a user to see his/her tab only.

Rajendra on May 03, 2020:

code not working proper way this file not open in my pc in for my user name

Ashish on April 28, 2020:

As I have write the script in module but it is still open in other IP address PC as well, I hope its not working...

Wael Rihani on November 19, 2019:

@Liam

Thank you for replying

I solved this by adding activeworkbook.save before activeworkbook.close.

But i had another issue.

I sent the file to another user to test.

When Excel run, a yellow warning on top telling that this file contains micros, and when choosing not to activate micro i get access to edit to micros to add uses or remove the code.

Liam (author) from UK on November 19, 2019:

@Wael Rihani

Im not sure why your excel is asking to save changes as no changes should have been made at this point. It should simply close unprompted. However, if this is happening, you could put "Thisworkbook.Close Save:=False" if access is FALSE and this should close the workbook unprompted

Wael Rihani on November 19, 2019:

I tried this and the code is working but there is a weakness, when user is not listed the message show but then when the activeworkbook.close run the message "want to save your changes" and by pressing cancel i can access the file.

Liam (author) from UK on August 31, 2019:

@ebersteve

In step 3, user will contain the username. You can then use "user" later and it's value will have the username in it. As for reversing the list. In step 5, swap the true and false on the access variables. So outside of the loop declare "access" as true first, then inside the loop make it false

ebersteve on August 30, 2019:

I am having troubles with this. I do not know where my user name will be displayed in step 3. Also I would like to do the opposite if this VBA. I would like whoever is on the list to not have access. Can you help?

bilal-sapphire on December 06, 2018:

Hi There,

I am new on VBA so I am not getting it that way.

Can i have a .xlsx file of this article at bilal.yousaf@sapphiretextiles.com.pk

Thanks

Bilal Yousaf