Soluciones provisionales para evitar intentar proteger condicionalmente las celdas en un libro de trabajo compartido

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
        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!


preguntado el 09 de marzo de 12 a las 15:03

2 Respuestas

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.

respondido 09 mar '12, 16:03

Thanks, will test today - I am already using a specified list of users and will try to separate module approach as I already have another couple of subs in a separate module... - Elatesummer

@Elatesummer - Ok, didn't know if the Environ() would be helpful or not. Also as an FYI, depending on how much changing of data is going on, this may trigger quite frequently, which puede o puede que no cause some slowdown in processing. - gafi

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

respondido 12 mar '12, 19:03

Thanks for your answer - Gaffi's answer did the trick though! - Elatesummer

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.