Restrict Workbook to Specific Users Only in Excel VBA
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.
Step 1 - Accessing VBA And Opening The Workbook Module
VBA can be accessed in one of two ways:
- Simply press ALT + F11
- 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
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
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!