+ Reply to Thread
Results 1 to 7 of 7

Vlookup Environ ("username") to get proper name and allocate protection

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Vlookup Environ ("username") to get proper name and allocate protection

    I have a spreadsheet that about 5 users have access to .
    I wish to have a macro that identifies the users full names based on the environ username in the first colomn and allows the user to control the spreadsheet dependant on their level of authority.

    So for example in sheet "Users" I have identified the following usernames with their corresponding full names.

    nichampness Nick Champness Full access (ie edit, delete rows etc)
    laclarke Linda Clarke Full access but no deletion capability
    ABloggs Andy Bloggs Autofilter access, edit access, no delete ability

    Anyone else who is not in the list above has read only access

    I want a msgbox to pop up, and for it to say "Welcome FULL NAME, you have INSERT ACCESS TYPE access"

  2. #2
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: Vlookup Environ ("username") to get proper name and allocate protection

    Hi NickMax1,

    as I was not sure which table should be protected the macro will protect sheet1 only. In case you would like to protect the "users" worksheet, you could simply replace sheet1 by users.

    Please regard that the accesslevels (as defined in column C of the users spreadsheet, would need to be spelled exactly as you have mentioned it below ( "Full access (ie edit, delete rows etc)", "Full access but no deletion capability" , "Autofilter access, edit access, no delete ability" ) otherwise the macro would set the level to "read only access" to makes sure!

    The macro should not be pasted into a module, but should be put directly into "ThisWorkbook" to ensure that it will run when the file is opened. For your reference I have attached a testfile


    Option Explicit
    Const PW = "YourPassword"
    
    Private Sub Workbook_Open()
        Dim AccLevel As String
        Dim FullName As String
        Dim x As Integer
    
        On Error Resume Next
        x = Application.Match(Application.UserName, Worksheets("Users").Columns("A"), 0)
        If IsNumeric(x) And x > 0 Then    'if user was found
            AccLevel = Worksheets("Users").Cells(x, 3)
            FullName = Worksheets("Users").Cells(x, 2)
        Else 'if username was not found
            AccLevel = "Read only Access"
        End If
        Select Case AccLevel
        Case "Full access (ie edit, delete rows etc)"
            Worksheets("Sheet1").Unprotect Password:=PW
    
        Case "Full access but no deletion capability"
            Worksheets("Sheet1").Protect Password:=PW, DrawingObjects:=False, Contents:=True, Scenarios:= _
                                         False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                                         AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
                                                                                               :=True, AllowInsertingHyperlinks:=True, AllowSorting:=True, _
                                         AllowFiltering:=True, AllowUsingPivotTables:=True
    
        Case "Autofilter access, edit access, no delete ability"
            Worksheets("Sheet1").Protect Password:=PW, DrawingObjects:=True, Contents:=True, Scenarios:=True _
                                       , AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
    
        Case Else
            AccLevel = "Read only Access"
            FullName = "Guest " & (Application.UserName)
            Worksheets("Sheet1").Protect Password:=PW, DrawingObjects:=True, Contents:=True, Scenarios:=True
        End Select
    
        MsgBox "Welcome " & FullName & " you have " & AccLevel, Title:="Welcome"
    End Sub
    In case you would require additional access levels. you could simply record a "manual protection" and adjust the code accordingly.
    Hope it works as expected

    Regards

    Theo
    Attached Files Attached Files
    Remember To Do the Following....
    1. Upload sample files
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  3. #3
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Vlookup Environ ("username") to get proper name and allocate protection

    Wow that is great Fettertiger.

    I had to change your Application.UserName commando Environ ("username") commands but other than that it works. How do I change this code so that it protects and unprotects all sheets in the workbook with the exception of "Start" and "Template" ?

  4. #4
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Vlookup Environ ("username") to get proper name and allocate protection

    anyone help with this?

  5. #5
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Vlookup Environ ("username") to get proper name and allocate protection

    basically what i mean is that currently the protection applies to Worksheets("Sheet1").Unprotect Password:=PW, but i want it to apply to ALL sheets in my workbook with the exception of Start and Template.

  6. #6
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: Vlookup Environ ("username") to get proper name and allocate protection

    Hi nickmax,

    sorry for not replying any earlier, but I was not online. Try this:

    Option Explicit
    Const PW = "YourPassword"
    
    Private Sub Workbook_Open()
        Dim AccLevel As String
        Dim FullName As String
        Dim x As Integer
        Dim ws As Worksheet
    
        On Error Resume Next
        x = Application.Match(Application.UserName, Worksheets("Users").Columns("A"), 0)
        If IsNumeric(x) And x > 0 Then    'if user was found
            AccLevel = Worksheets("Users").Cells(x, 3)
            FullName = Worksheets("Users").Cells(x, 2)
        Else    'if username was not found
            AccLevel = "Read only Access"
        End If
        Select Case AccLevel
        Case "Full access (ie edit, delete rows etc)"
            For Each ws In ThisWorkbook.Worksheets
                ws.Unprotect Password:=PW
            Next
        Case "Full access but no deletion capability"
            For Each ws In ThisWorkbook.Worksheets
                ws.Protect Password:=PW, DrawingObjects:=False, Contents:=True, Scenarios:= _
                           False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
                           AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
                                                                                 :=True, AllowInsertingHyperlinks:=True, AllowSorting:=True, _
                           AllowFiltering:=True, AllowUsingPivotTables:=True
            Next
        Case "Autofilter access, edit access, no delete ability"
            For Each ws In ThisWorkbook.Worksheets
                ws.Protect Password:=PW, DrawingObjects:=True, Contents:=True, Scenarios:=True _
                         , AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True
            Next
        Case Else
            AccLevel = "Read only Access"
            FullName = "Guest " & (Application.UserName)
            For Each ws In ThisWorkbook.Worksheets
                ws.Protect Password:=PW, DrawingObjects:=True, Contents:=True, Scenarios:=True
            Next
        End Select
    
        MsgBox "Welcome " & FullName & " you have " & AccLevel, Title:="Welcome"
    End Sub
    Just change your environ routine as you did before and it should work


    Regards

    Theo

  7. #7
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Vlookup Environ ("username") to get proper name and allocate protection

    works brilliantly!

    solved
    Last edited by nickmax1; 10-04-2012 at 04:38 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1