I would like to set up a macro to restrict an Excel file to a specific user from the list

0

I have an excel file which I would like to restrict it for only a certain users to view so because there are multiple files I would like to setup a macro which will restrict the files to a certain users which I will provide on a list.

Bruce Wayne

Posted 2019-06-18T07:31:54.177

Reputation: 1

1Assuming you're using/going to use VBA. What have you tried so far? Where have you got stuck? – spikey_richie – 2019-06-18T07:34:45.327

Answers

3

You DO NOT WANT TO DO THIS.

To prevent users from opening a file require them to open the file in order the macro to run (and if not the right user has opened it then close it immediately).
Besides it being annoying for the user there is not a safe way for it, if users starts the file without macros enabled then the macro just can't run and the file will open normally.

Instead look for blocking access to the file itself, depending on the technology you use. Most file sharing solution can manage access quite well.

Máté Juhász

Posted 2019-06-18T07:31:54.177

Reputation: 16 807

0

"Protection" in Excel is no protection at all against a moderately experienced VBA user. It's so easily circumvented with readily available password hacks.

If you can be sure that all your users are naïve then there are strategies to give your workbook access to a certain group of users while excluding everyone else. As you haven't shown any code I'll give a few hints but leave it to you to patch it into a workable solution.

Password protect your workbook and VBA Project with different passwords. Different so users need 2 separate password hacks to get to your hidden sheets.

Hide all sheets except for an "Enable Macros" sheet before every save. By hide I mean set the sheets Visible property to xlVeryHidden. This setting masks the sheet name from Home > Format > Hide & Unhide > Sheets. Right-clicking a sheet tab will have the Unhide... option greyed out. Makes it hard for the casual user to even see the hidden sheets. If you apply sheet protection to the sheets their names may be exposed in the Backstage.

Build a function to convert Windows username to an 8 character string comprising the initial of firstname and first 7 digits of surname ignoring spaces in surname. I use:

Function UserShortName()
    User = Application.UserName
    If InStr(1, User, " ") Then
        UserShortName = LCase(Left(Left(User, 1) _
        & Replace(Mid(User, InStr(1, User, " ") + 1), " ", ""), 8))
    Else
        UserShortName = User
    End If
End Function

I don't use Apllication.UserUame because that can be changed fairly easily to impersonate someone else.

Build a table/range or an in-code array of UserShortNames of AllowedUsers.

Have a VisibilitySettings procedure that can be called from Workbook_AfterSave and Workbook_Open events. This sub should test the UserShortName against the AllowedUsers table/range/hard-coded names. VisibilitySettings will make selected sheets visible.

When a user opens the workbook they will see the "Enable Macros" if they don't have macros enabled. If they have macros enabled they will see whatever you allow in VisibilitySettings.

Mark Fitzgerald

Posted 2019-06-18T07:31:54.177

Reputation: 502

-2

The below shown VBA (Macro) code will help you to create a Workbook, will automatically check the Window Logged in users, and allow or disallow access to the Workbook.

Private Sub Workbook_Open()

Application.AutomationSecurity = msoAutomationSecurityLow

Dim user As String
Dim users(4) As String

users(0) = "User1"
users(1) = "User2"
users(2) = "User3"
users(3) = "User4"


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 ("Opps, user """ & "user" & """ does not have access to open this workbook")
    ActiveWorkbook.Close
End If

End Sub

N.B.

  • Save the Workbook as Excel-Macro Enabled Workbook with .Xlsm extension.
  • If Macro is Disabled using File, Option, Trust Center, Marco Settings or by any method, then this Code line will Enables the Macro.

Application.AutomationSecurity = msoAutomationSecurityLow

  • In the code, User Names are editable & all must be as similar as Windows User's Profile of that particular Computer.

Rajesh S

Posted 2019-06-18T07:31:54.177

Reputation: 6 800

How safe is it? What if users will just disable macros before opening the workbook? – Máté Juhász – 2019-06-18T09:18:10.090

@MátéJuhász, this is full proof method,, needless to work with Workbook which is not Macro Enabled !! – Rajesh S – 2019-06-18T09:25:57.757

Macros can be disabled even in macro enabled workbook... "macro enabled" just means it can store macros, but there is not guarantee they will actually run... – Máté Juhász – 2019-06-18T09:27:51.713

@MátéJuhász, why anyone disable the Macro since using Macro Enable Event and for crucial method,, it would like commit a deliberate mistake !! – Rajesh S – 2019-06-18T10:10:09.197

@MátéJuhász,, if user or someone has disabled the Macro,, before Open the Workbook, VBA code will enable it, I've edited the code and included the command for it !! – Rajesh S – 2019-06-18T10:43:44.183

1

I agree with @MátéJuhász, I have macro-enabled workbooks shared on network drives and anyone can bypass it. If you don't click the Enable Content button the macro will not run and the file will be accesible to anyone.

– CaldeiraG – 2019-06-18T10:55:25.037

@CaldeiraG,, that's the reason I've edited the Code and included the VBA code line on top will Enable the Macro by resetting the Macro Disable option,, check the post and N.B. (caveat) section. – Rajesh S – 2019-06-18T10:59:55.670

@RajeshS: if macros are disabled, then your code won't run and won't disable itself. – Máté Juhász – 2019-06-18T11:02:03.237

@RajeshS thanks for pointing out, i see the line now. That might work but it's still a not-so-efficient approach, not wrong though. – CaldeiraG – 2019-06-18T11:02:48.187

@MátéJuhász,, if macros are disabled, then as soon user open the Workbook,, first Excel show the standard Yellow like message Enable Contents and as soon user Click it,, Workbook Open Event will RUN the code !! – Rajesh S – 2019-06-18T11:06:29.120

@CaldeiraG,, try the code will work ☺ – Rajesh S – 2019-06-18T11:07:25.700

1In an ideal word users would know they're not supposed to open the file so won't open it. In the real word they want to open it and won't click "enable". This code is NOT A SOLUTION!! Also after clicking "enable" Application.AutomationSecurity will don't change anything, without clicking "Enable" it won't run, so I even more don't understand know why it is needed. – Máté Juhász – 2019-06-18T11:08:50.933

@MátéJuhász, if Macro is been disabled then not only this but non of the Macros will work,,& it's a non-professional approach, why any one even think to do so, since Macro is crucial part of computing !! – Rajesh S – 2019-06-18T16:58:56.673