Dear Excel gurus,
I am new to VBA, and I would like to write such a code that when a user types "-" sign in the cells in the Column number 11 (K2:K100), the only current cell can be locked automatically, not all cells below the current one. The other cells can be locked after the "-" sign is entered again. I written down the code below so that you can help me. I searched a lot on internet but could not find the exact solution to my problem. I appreciate any help in advance.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Value = "-" Then
Dim Source As String
Source = "Do you want to unlock the password"
changeInput = MsgBox(Source, vbYesNo + vbQuestion, "Unlock the sheet")
If changeInput = vbYes Then
Dim pass As String
pass = InputBox("Enter the password")
If pass <> "test" Then
MsgBox ("Wrong Password")
Else
ActiveSheet.Unprotect Password:="test"
Target.Locked = False
End If
End If
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
ActiveSheet.Unprotect Password:="test"
Set cel = ActiveSheet.Range("K2:K100")
If cel = "-" Then
check = MsgBox("The cell will be locked after "-" sign.", vbYesNo, "Warning")
If check = vbYes Then
cel.Locked = True
Else
cel.Value = ""
End If
End If
'ActiveSheet.Protect Password:="test"
End Sub
I am new to VBA, and I would like to write such a code that when a user types "-" sign in the cells in the Column number 11 (K2:K100), the only current cell can be locked automatically, not all cells below the current one. The other cells can be locked after the "-" sign is entered again. I written down the code below so that you can help me. I searched a lot on internet but could not find the exact solution to my problem. I appreciate any help in advance.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Value = "-" Then
Dim Source As String
Source = "Do you want to unlock the password"
changeInput = MsgBox(Source, vbYesNo + vbQuestion, "Unlock the sheet")
If changeInput = vbYes Then
Dim pass As String
pass = InputBox("Enter the password")
If pass <> "test" Then
MsgBox ("Wrong Password")
Else
ActiveSheet.Unprotect Password:="test"
Target.Locked = False
End If
End If
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
ActiveSheet.Unprotect Password:="test"
Set cel = ActiveSheet.Range("K2:K100")
If cel = "-" Then
check = MsgBox("The cell will be locked after "-" sign.", vbYesNo, "Warning")
If check = vbYes Then
cel.Locked = True
Else
cel.Value = ""
End If
End If
'ActiveSheet.Protect Password:="test"
End Sub