VBA Code to allow only the selected users to open the file

  • Hi,


    I have a dashboard along with the below code. I want only few (selected) users (Only Managers) to have access (Read/Write) to this file and the code should restrict the other users from opening the file, it should display a message saying 'Hi, You don't have access to view this dashboard. Please contact your Manager for assistance'.


    Something like this code


    Code
    1. If Environ("USERNAME") = "xxxxxx" Or Environ("USERNAME") = "yyyyyyyy" Or Environ("USERNAME") = "zzzzzzzzzzzz" Or Environ("USERNAME") = "uuuuuuuuuuu" Then
    2. Else
    3. MsgBox "'Hi, You don't have access to view this dashboard. Please contact your Manager for assistance", vbCritical
    4. Exit Sub


    Please find the below code for the dashboard and help me in completing the code and at which level this should be applied.


    I don't if this is possible?


  • Re: VBA Code to allow only the selected users to open the file


    You could simply password protect the workbook. If you want to use VBA code then take a look at adapting the password form


    PasswordForm example

  • Re: VBA Code to allow only the selected users to open the file


    Thanks royUK.


    But my requirement is as below.


    I want to let only the Managers (I will add the user ID's in the code itself) to open the file and, even if the managers send this file to others then I don't want others to even open the file (when they double click on it) as they don't have the access.


    Is this possible?


    Only the UserIDs of the users which I have mentioned in the code should be able to open the file others should get a notification.

  • Re: VBA Code to allow only the selected users to open the file


    Hi S O,


    I am not able to add the Username code to my dashboard Code, please help me where I can add the code so that it will only allow the user ID's in the code to open the file. For Others a text should be displayed.

  • Re: VBA Code to allow only the selected users to open the file


    [wbc]*[/wbc]


  • Re: VBA Code to allow only the selected users to open the file


    Quote from S O;753003

    [wbc]*[/wbc]




    Hi S O,


    Thanks it worked like a charm.


    royUK,


    Thanks for the suggestion, but I am not aware of that

  • Re: VBA Code to allow only the selected users to open the file


    I would make a note of royUK's suggestion though and stick with a password. Environment variables can be changed if you know how and so this isn't a secure approach. I could quite easily take a guess at someone's username in work and gain access on this basis but passwords are a tried and tested security method.


    Basically, don't re-invent the wheel - just use a password...