Consulting

Results 1 to 15 of 15

Thread: VBA to Restrict certain usernames.

  1. #1

    VBA to Restrict certain usernames.

    Hello,

    Very (extremely) new to VBA. I have tried and googled a lot for this but cannot seem to get anything to work remotely close to what I want (if it is possible).

    At work we use a shared workbook for logging tests we perform during the day. The problem is that not everyone has their own user login (e.g. new starters) and they use accounts that all start with "Tester".

    Is there a way to lock out any username that starts with "Tester" to prevent using the workbook via VBA? And without typing in every user's name into a sheet.

    I did read another post on this forum
    /showthread.php?34919-Solved-Restricting-Access-to-Spreadsheet-by-Network-User-Name-ID
    but it didn't make any sense to me of what did what and what goes where

    Thank you in advance,

    RR
    Last edited by realrookie; 03-01-2015 at 05:05 AM. Reason: Adding info

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    The absolute easiest and therefore the least secure is a sub in the Workbook_Open event. It is easily bypassed, so it depends on how complicated you want to get.

    Look at the attached xlsm for example

    Ask again if you need some more robust solutions
    Attached Images Attached Images
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Thank you very much Paul.

    This does what I wanted. If possible a little bit more robust, that would be great. I tried adding Test as well:
    'sUserName = "TESTER1"
    'sUserName = "TEST1"
    But this did not seem to accept the Test (not Tester) account.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Private Sub Workbook_Open()
        Dim sUserName As String
        Dim vecExclude As Variant
        
        sUserName = Environ("USERNAME")
        vecExclude = Array("Tester1", "Test1", "bob")
        
        If Not IsError(Application.Match(sUserName, vecExclude, 0)) Then
        
            Call MsgBox("SORRY!!!" & vbCrLf & vbCrLf & "But '" & sUserName & "' logons are not allow to use this" & _
                vbCrLf & vbCrLf & "The workbook will now close", _
                vbCritical + vbOKOnly, "Tester Check")
        
            ThisWorkbook.Close (False)
        End If
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @XLD:

    Quote Originally Posted by xld View Post
            ThisWorkbook.Close (False)
    Why the parenthesis in this case?

    Mark

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Sorry, blond and slow witted...

    Also, why the Call for the message box?

    Mark

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    @ Mark, You dyed your mane?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Aussiebear View Post
    @ Mark, You dyed your mane?
    Not so far at least. Turned 54 last December...

    I am afraid the black mane is Malcom's fault!

    Mark

  9. #9
    Quote Originally Posted by GTO View Post
    Why the Call for the message box?

    Mark
    So that the user knows why they have no access to it.

  10. #10
    Thank you xld for the code, but it does not seem to have any effect. I copied your code in This.Workbook but with our temporary accounts I can still access the file.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by GTO View Post
    @XLD:

    Why the parenthesis in this case?

    Mark
    No idea, that was already in the code, I just copied it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @XLD:


    ACK!

    and ...



    My bad. I think an accurate expression over there would be that I've obviously become a lazy sod and a dullard. I failed to look up and see the original code.

    Hope all is well in your corner,

    Mark

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    The marked line looks for TESTER+anything as a user id and exits if it does not start with it (actually "TESTER" in the first 6 char), allowing the workbook to open


    Option Explicit
    Private Sub Workbook_Open()
        Dim sUserName As String
        
        sUserName = UCase(Environ("USERNAME"))
        
        'uncomment out for testing
        'sUserName = "TESTER1"
        
        If Left(sUserName, 6) <> "TESTER" Then Exit Sub   ' <--------------------------------------
        
        Call MsgBox("SORRY!!!" & vbCrLf & vbCrLf & "But 'Tester' logons are not allow to use this" & _
            vbCrLf & vbCrLf & "The workbook will now close", _
            vbCritical + vbOKOnly, "Tester Check")
        ThisWorkbook.Close (False)
    End Sub

    If you change it to

     If Left(sUserName, 4) <> "TEST" Then Exit Sub

    then it will allow the workbook to open if the first 4 char of user id are not TEST

    Did you want TEST1 to be allowed to use it, but TESTERxx not?


    Another way to look at it is to not use the Exit Sub and just a If / Then

    Option Explicit
    Private Sub Workbook_Open()
        Dim sUserName As String
        
        sUserName = UCase(Environ("USERNAME"))
        
        'uncomment out for testing
        sUserName = "TEST1"
        
        If Left(sUserName, 4) = "TEST" Then
        
            Call MsgBox("SORRY!!!" & vbCrLf & vbCrLf & "But 'Tester' logons are not allow to use this" & _
                vbCrLf & vbCrLf & "The workbook will now close", _
                vbCritical + vbOKOnly, "Tester Check")
            ThisWorkbook.Close (False)
        End If
        
    End Sub
    Last edited by Paul_Hossler; 03-03-2015 at 07:26 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  14. #14
    Thank you Paul, that works fine, but now I get an error with Private Sub Workbook_Open() as it appears 2 times. Once for this code and second to start a timer which saves and closes the file after 20min. Which gives me headaches as well now, but that is a different topic.

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Well, you only get one WB Open

    I didn't follow all of your PM, but I'd expect you want to call SetTime only if the WB actually opens


    Option Explicit
    Private Sub Workbook_Open()
        Dim sUserName As String
        
        sUserName = UCase(Environ("USERNAME"))
        
        'uncomment out for testing
        sUserName = "TEST1"
        
        If Left(sUserName, 4) = "TEST" Then
        
            Call MsgBox("SORRY!!!" & vbCrLf & vbCrLf & "But 'Tester' logons are not allow to use this" & _
                vbCrLf & vbCrLf & "The workbook will now close", _
                vbCritical + vbOKOnly, "Tester Check")
    
            'may be needed ???
            Call Disable
    
            ThisWorkbook.Close (False)
        End If
    
    
    'if you get this far then the user id is OK
    Call SetTime
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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