i googled some vba code for what i want to do. but in trying to put a few commands together. what i want to do doesnt work.
i have a 5 column worksheet, one page worth. 1- i want to lock column C when data is entered. i also want to ask a question whether the data is okay before locking. 2- when the 4 columns are filled in , i want to automatically record user with date and time in the 5th column.
the below asks me the question for every column field , not just column C that i want to lock.
and the user/date fills in for all lines 7 thru 44 in column e , not just the current line i am entering on.
and then the excel freezes
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel_c As Range
Dim cel_e As Range
On Error Resume Next
Set cel_c = Intersect(Range("c7:c44"), Target)
Set cel_e = Intersect(Range("e7:e44"), Target)
For Each cel_c In Target
If cel_c.Value <> "" Then
check = MsgBox("Is this entry correct? This cell cannot be edited after entering a value.", vbYesNo, "cell lock definition")
If check = vbYes Then
Target.Worksheet.Unprotect Password:="secret"
cel_c.Locked = True
cel_e = Format(Date, "mm/dd/yyyy") & " at " & Format(Time, "hh:mm AMPM") & " by " & Application.UserName
Else
cel_c.Value = ""
Target.Worksheet.Protect Password:="secret"
End If
End If
Next cel
End Sub
i have a 5 column worksheet, one page worth. 1- i want to lock column C when data is entered. i also want to ask a question whether the data is okay before locking. 2- when the 4 columns are filled in , i want to automatically record user with date and time in the 5th column.
the below asks me the question for every column field , not just column C that i want to lock.
and the user/date fills in for all lines 7 thru 44 in column e , not just the current line i am entering on.
and then the excel freezes
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel_c As Range
Dim cel_e As Range
On Error Resume Next
Set cel_c = Intersect(Range("c7:c44"), Target)
Set cel_e = Intersect(Range("e7:e44"), Target)
For Each cel_c In Target
If cel_c.Value <> "" Then
check = MsgBox("Is this entry correct? This cell cannot be edited after entering a value.", vbYesNo, "cell lock definition")
If check = vbYes Then
Target.Worksheet.Unprotect Password:="secret"
cel_c.Locked = True
cel_e = Format(Date, "mm/dd/yyyy") & " at " & Format(Time, "hh:mm AMPM") & " by " & Application.UserName
Else
cel_c.Value = ""
Target.Worksheet.Protect Password:="secret"
End If
End If
Next cel
End Sub