Elatesummer
Elatesummer

Reputation: 313

Workarounds to avoid trying to conditionally protect cells in a shared workbook

Now...I have a workbook with 5 tabs. On tab 1 (the dashboard) I have four buttons - each one is basically a link to one of the other four. The reason it's there with buttons is that only users with 'admin' permission to the workbook can go and change data on the other four tabs.

All other users should not be allowed to change any data on those other four tabs. It's a "look but don't touch" policy. Oh - to complicate it a little, we then decided they SHOULD be able to change one column on each worksheet.

So. It's a shared workbook. My piece of code:

Private Sub cmdViewHistology_Click()
    If UserPermsLevel = "High" Or UserPermsLevel = "Super" Then
    Worksheets("Histology and Cytology").Visible = True
    Worksheets("Histology and Cytology").Activate
        Exit Sub
    ElseIf (UserPermsLevel = "Normal" Or UserPermsLevel = "Normal and UserName") Then
        Worksheets("Histology and Cytology").Visible = True
        Worksheets("Histology and Cytology").Range("A:I").Locked = True
        Worksheets("Histology and Cytology").Range("J:J").Locked = False
        Worksheets("Histology and Cytology").Protect DrawingObjects:=False, Contents:=True, Scenarios:=False
        Worksheets("Histology and Cytology").Activate
    Else
        MsgBox "Sorry, this command is not available."
    End If
End Sub

It doesn't work. Because it's shared, I know I can't protect the sheet - which is a shame. The problem is...I need those admin users to be able to change everything, and I need everyone else to be able to look but not touch!

So...does anyone have an alternative approach I could use for this? At the moment my workaround is to disable the four buttons and give them an out of order message which is not winning me any favours...

Really really hoping someone has a bright idea which will help me out of this hole!

Thanking you

Upvotes: 3

Views: 668

Answers (2)

datatoo
datatoo

Reputation: 2049

Perhaps checking on the SelectionChange event if ActiveCell.Column is whatever you want to allow and UnProtect the sheet, any other column would assert protection. Keep things otherwise locked to all but admins. --I meant to suggest that @Gaffi check for UserPermsLevel is a good idea to incorporate as well

Upvotes: 1

Gaffi
Gaffi

Reputation: 4367

If you know the Windows login for the admin users (or non-admin, whichever is easer) you can use Environ("Username") to check against a list of approved users.

On the change event of the worksheet, find out what cell is being changed, and if it's not allowed, then check to see if that specific user has rights to do so.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim EditableRange As Range
Dim Intersect As Range
    Set EditableRange = Range("A1") ' Set your range appropriately...
    Set Intersect = Application.Intersect(EditableRange, Target)
    If Intersect Is Nothing Then
        'If this is not in the list of editable cells, then check for admin status
        If UserPermsLevel <> "High" And UserPermsLevel <> "Super" Then
            Application.EnableEvents = False
            Application.Undo 'Undo the edits the user made
            Application.EnableEvents = True
            MsgBox "Sorry, this command is not available."
        End If
        'Implied else: allow change
    End If
    'Implied else: allow change

End Sub

Also, you will need to set this code on each of your 'uneditable' tabs, or put it in a separate module which is then called by the Private Sub Worksheet_Change(ByVal Target As Range) on each of those tabs, not on the code for the buttons that switch the visible tab.

Upvotes: 1

Related Questions