Hi Duncan,
Serves me right for throwing you a down and dirty code. :O>
I've changed the code significantly, added password protection, etc to the
code to make it a lot more robust.
Because of this, I have added two macros.
'Protect_Unprotect_TheCells'
'ProtectTheCells'
'Protect_Unprotect_TheCells' is the general routine that protects /
unprotects the worksheet so that the cell protection/unprotection can most
easily be accomplished.
This routine then calls the 'ProtectTheCells' routine which is what you are
most interested in.
I have taken the programming out of the 'Worksheet_SelectionChange' event
to make the flow better but more importantly, if you want to unportect the
worksheet for a period of time while you edit it, you can easily simply
comment out the one line of code..."Call Protect_Unprotect_TheCells" instead
of having to comment out a whole slew of lines.
Hope this is what you're looking for. If you want to reach me directly, I
am currently consulting at ge.com, see my email below (take out the _NOSPAM
part).
HTH,
--
Gary Brown
gary_brown@ge_NOSPAM.com
If this post was helpful, please click the ''''Yes'''' button next to
''''Was this Post Helpfull to you?".
'/============================================/
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call Protect_Unprotect_TheCells
End Sub
'/============================================/
Private Sub ProtectTheCells()
Dim rCell As Range, rngB As Range
On Error GoTo err_Sub
Set rngB = Range("B:B")
'alternate to looking at all cells in Col B including
' possible headers that you don't want protected is...
' Set rngB = Range("B2:B10000")
'loop through all cells in Col B
For Each rCell In rngB
'in order to not check all 65000 lines each time
' check to see if you are inside 'used' part
' of worksheet. If not, stop processing
If TypeName(Application.Intersect(rCell, _
(ActiveSheet.UsedRange))) = "Nothing" Then
Exit For
End If
'check all cells in Col B for an entry in each cell
If Len(rCell.Value) <> 0 Then
'if there is an entry, protect the cell in Col A
rCell.Offset(0, -1).Locked = True
Else
'if there is NO entry, unprotect the cell in Col A
rCell.Offset(0, -1).Locked = False
End If
Next rCell
exit_Sub:
On Error Resume Next
Set rngB = Nothing
Exit Sub
err_Sub:
GoTo exit_Sub
End Sub
'/============================================/
Private Sub Protect_Unprotect_TheCells()
'template for unprotecting/protecting worksheet
Dim blnProtectContents As Boolean
Dim blnProtectDrawingObjects As Boolean
Dim blnProtectScenarios As Boolean
Dim strPassword As String
'set default for whether worksheet is protected or not
blnProtectContents = False
blnProtectDrawingObjects = False
blnProtectScenarios = False
strPassword = ""
'check if worksheet is unprotected
' if it's protected, get various information
On Error Resume Next
If Application.ActiveSheet.ProtectContents = True Then
blnProtectContents = True
If Application.ActiveSheet.ProtectDrawingObjects = True Then
blnProtectDrawingObjects = True
End If
If Application.ActiveSheet.ProtectScenarios = True Then
blnProtectScenarios = True
End If
'try to unprotect worksheet
ActiveSheet.Protect Password:=strPassword, _
DrawingObjects:=False, _
Contents:=False, _
Scenarios:=False
'if try to unprotect worksheet didn't work
' then ask for password
If Application.ActiveSheet.ProtectContents = True Then
'still protected so try password
strPassword = InputBox("Enter Password: " & vbCr & vbCr & _
"If there is no password, press ENTER." & vbCr & vbCr & _
"ONLY enter Password if source of this macro is TRUSTED!!!", _
"Password to Unprotect Worksheet...", "")
ActiveSheet.Unprotect Password:=strPassword
'password didn't work - still not unprotected so stop process
If Application.ActiveSheet.ProtectContents = True Then
Exit Sub
End If
End If
End If
On Error GoTo 0
'call the desired routine
Call ProtectTheCells
'set worksheet back to original protected/unprotected state
On Error Resume Next
ActiveSheet.Protect Password:=strPassword, _
DrawingObjects:=blnProtectDrawingObjects, _
Contents:=blnProtectContents, Scenarios:=blnProtectScenarios
End Sub
'/============================================/