Lock a cell in a column when a user types "-" character

Shirin301

New Member
Joined
May 19, 2020
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try this combination
- note variable & constant declared at TOP of code to make available to all procedures in sheet code

Place in sheet code window
VBA Code:
Option Explicit
Private Const pw = "test"
Private T As Range

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim msg As String: msg = "The cell will be locked after  " & Chr(34) & " - " & Chr(34) & " sign"
    If Not Intersect(Target, Range("K2").Resize(Me.Rows.Count - 1)) Is Nothing Then
        Set T = Target.Resize(1, 1)
        If T = "-" Then
            Select Case MsgBox(msg, vbYesNo, "Warning!")
                Case vbYes
                    Me.Unprotect pw
                    T.Locked = True
                    Me.Protect pw
                Case Else: Target.ClearContents
            End Select
        End If
    End If
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Const source = "Do you want to unlock cell with password"
    Const wrong = "wrong password"
    Set T = Target.Resize(1, 1)
    If Not Intersect(Target, Range("K2").Resize(Me.Rows.Count - 1)) Is Nothing Then
        If T = "-" Then
            If MsgBox(source, vbYesNo + vbQuestion, "Unlock the cell?") = vbYes Then
                Select Case InputBox("Enter the password")
                    Case pw
                        Me.Unprotect pw
                        T.Locked = False
                        T.ClearContents
                        Me.Protect pw
                    Case Else: MsgBox wrong
                End Select
            End If
        End If
    End If
End Sub
 
Upvote 0
Thanks for your prompt response, but interestingly, when I put "-" sign in that column, all cells will get locked. I want to lock seperately each cell by entering minus sign.
 
Upvote 0
Try this minor amendment

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim msg As String: msg = "The cell will be locked after  " & Chr(34) & " - " & Chr(34) & " sign"
    Set T = Target.Resize(1, 1)
    If Not Intersect(Target, Range("K2").Resize(Me.Rows.Count - 1)) Is Nothing Then
        If T = "-" Then
            Select Case MsgBox(msg, vbYesNo, "Warning!")
                Case vbYes
                    Me.Unprotect pw
                    T.Locked = True
                    Me.Protect pw
                Case Else: Target.ClearContents
            End Select
        End If
    End If
End Sub
 
Upvote 0
Interestingly, the cells until 6th cell are unlocked. Is it supposed to include a range of K2:K1,048,576 ? My starting cell is K2.
 
Upvote 0
not unlocked sorry locked*

That is a garbled message!
- no idea what you are saying :unsure:


Before any further testing manually unlock all cells in column K and then re-edit each cell
- it works for me and only locks ONE cell
- I suspect any problem was left behind by the first test
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,984
Members
449,276
Latest member
surendra75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top