View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Dynamic unlock a cell (under condition)

What did you want to do with the first _selectionchange event. I'm not sure it
does what you want.

And the second routine. You'll want to stop any changes you make from calling
this routine over (and over and over)... You can do that by controlling
"application.enableevents".

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

Dim myPWD As String

myPWD = "hi"

If Intersect(Target, Me.Range("e7:e56,n7:n56")) Is Nothing Then
Exit Sub
End If

Me.Unprotect Password:=myPWD
Application.EnableEvents = False
If IsEmpty(Target.Value) Then
Target.Offset(, 3).ClearContents
Else
With Target.Offset(, 3)
Select Case Left(Target.Value, 1)
Case "1", "2", "3": .Locked = True: .ClearContents
Case "4", "6": .Locked = False: .ClearContents
Case "5": .Locked = True: .Value = "SI"
End Select
End With
End If
Application.EnableEvents = True
Me.Unprotect Password:=myPWD

End Sub


And Left() returns a string. I changed 1,2,3 to "1","2","3". Excel/VBA didn't
seem to care, but I do <bg.



MyVi wrote:

Hello people, so far I'm trying with thise code, but something seems to
be wrong 'cause still does not work.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("b7:b56,k7:k56")) Is Nothing Then
ActiveCell.Select
End Sub

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Intersect(Target, Range("e7:e56,n7:n56")) Is Nothing Then Exit Sub
If IsEmpty(Target) Then Target.Offset(, 3).ClearContents: Exit Sub
With Target.Offset(, 3)
Select Case Left(Target, 1)
Case 1, 2, 3: .Locked = True: .ClearContents
Case 4, 6: .Locked = False: .ClearContents
Case 5: .Locked = True: .Value = "SI"
End Select
End With

End Sub

&

Private Sub Workbook_Open()
Worksheets("Sheet3").Protect Password:="aBc", UserInterfaceOnly:=True
End Sub

ANY IDEA?

Thank you

Victor


--

Dave Peterson