Visually check Protection code

S

Stuart

I wish to check that a complicated (for me) protection routine is
coded and operating correctly.

Addin code allows a User to select their workbook(s) and then
continues to open their selection. As it does so, code ends with
.Protect
.EnableSelection = xlUnlockedCells

Can I say something like:

Sub Test_Protection()
Dim ws As Worksheet, c As Range
For Each ws In ActiveWorkbook.Worksheets
With ws
For Each c In .UsedRange.Columns("A:G")
If Not c.Locked = True Then
'User is permitted to edit this cell, so color it:
c.Interior.ColorIndex = 34
End If
Next
End With
Next
End Sub

This doesn't work for me.

Regards.
 
S

Stuart

Thanks, but I had tried that. It unprotected the sheet
but my code failed to change the cell's colour.

Setting up cell "B45" and running this amended code
seems to work:
Sub Test_Protection2()
Dim ws As Worksheet, c As Range
For Each ws In ActiveWorkbook.Worksheets
With ws
.Unprotect
' For Each c In .UsedRange.Columns("A:G")
' If Not c.Locked = True Then
' 'User is permitted to edit this cell, so color it:
' c.Interior.ColorIndex = 34
' End If
' Next
If Not .Range("B45").Locked = True Then
.Range("B45").Interior.ColorIndex = 34
End If
End With
Next
End Sub

Now why might this work, but not the first version, please?

Regards.
 
S

Stuart

Apologies, because I think the error lies in the original
"protection" routine. I had been trying to avoid selecting
each worksheet before applying the "protection" code.
Looks as though avoiding 'select' results in the code
failing to protect the sheet.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top