Consulting

Results 1 to 7 of 7

Thread: Removing a Users access to a Shared Workbook

  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,406
    Location

    Removing a Users access to a Shared Workbook

    I recently came across this code and was wondering if it removes a particular User or all Users of a Shared workbook?
    Sub Remove_User()
    Dim UsrList()
    UsrList = ThisWorkbook.UserStatus
    For i = 1 To UBound(UsrList)
        ThisWorkbook.RemoveUser (i)
    Next
    End Sub
    I'm assuming the Owner of the Workbook, maintains a list (UsrList) of those who can access (Read & Write) the workbook somewhere.
    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

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,837
    Location
    Sounds like it

    https://learn.microsoft.com/en-us/of...6)%26rd%3Dtrue

    I went to File, Share and it asks where to put it and who can read or edit it

    Never really tried
    ---------------------------------------------------------------------------------------------------------------------

    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
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,406
    Location
    Thank you Paul.
    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

  4. #4
    Administrator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,278
    Location
    I think, the way the code is created, it will remove all users. Reason being that 'Workbook.UserStatus' creates an array of all users currently in the workbook:
    https://learn.microsoft.com/en-us/of...ook.userstatus

    The loop then proceeds to remove every user that is in the array, therefore removing every user.

    This would have the same effect as unsharing the workbook and then resharing it (this is what I do to files at my workplace)

    I use shared workbooks at work with VBA, the only way to get to the VBA in a shared workbook is to unshare it, you are not able to edit/ view code while the workbook is shared.

    To remove a specific user would be something like the below (Untested)
    Sub Remove_User()
        Dim UsrList()
        
        UsrList = ThisWorkbook.UserStatus
        
        For i = 1 To UBound(UsrList)
            If UsrList(i, 1) = "Joe Bloggs" Then
                ThisWorkbook.RemoveUser (i)
            End If
        Next
    End Sub
    Not sure if the 'UsrList(i, 1)' should be 'UsrList(i)' or 'UsrList(i, 1)' as i can't test due to all of my shared workbooks being full of users as i work for a 24 hour company.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2408, Build 17928.20080

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,406
    Location
    Thank you Georgiboy. My first impression was that it removed everyone, but a workbook needs an owner, since the current user must then become the default owner who is able to control the read & write permissions. However I wasn't sure, so didn't try and test it.

    Ive seen a slightly different version of removing a user where it didn't use the user name but a position on the list.

    Sub Remove_User()
        Dim UsrList()
        
        UsrList = ThisWorkbook.UserStatus
        
        For i = 1 To UBound(UsrList)
            If UsrList(i, 1) = User.2 Then
                ThisWorkbook.RemoveUser (i)
            End If
        Next
    End Sub
    


    The
    disadvantage with this one is that you could potentially knock the wrong person out of the workbook if you got the position count wrong.
    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

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,837
    Location
    What is "User.2" ?
    ---------------------------------------------------------------------------------------------------------------------

    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

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,406
    Location
    Sorry Paul but I didn't remember it as well as I should have....

    The code was
    Sub Remove_User1()
    Dim UsrList()
    UsrList = ThisWorkbook.UserStatus
    If UBound(UsrList) > 1 Then
        ThisWorkbook.RemoveUser (2)
    End If
    End Sub
    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

Posting Permissions

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