Lock cells after entry is made

y3kesprit

Board Regular
Joined
Mar 23, 2010
Messages
133
Hello all,

I am trying to use a macro that locks cells V5:V2004 after users have entered data into them.

I have tried the following:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    '   Code goes in the Worksheet specific module
    Dim rng As Range
        '   Set Target Range, i.e. Range("A1, B2, C3"), or Range("A1:B3")
        Set rng = Target.Parent.Range("V5:V2004")
             '   Only look at single cell changes
            If Target.Count > 1 Then Exit Sub
            '   Only look at that range
            If Intersect(Target, rng) Is Nothing Then Exit Sub
            '   Action if Condition(s) are met (do your thing here)
            ActiveSheet.Unprotect "manfield"
                Target.Locked = True
            ActiveSheet.Protect "PasswordGoesHere"
End Sub

This works fine. However, I prefer a macro that enables me to click again on the filled-up cell where it would prompt me to enter the password so that I can made changes (and thereafter lock the cell again) The code above does not allow that.

Is this possible? Would appreciate the help! :)
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hello,

Maybe add a selection change event like:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_SelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <br>    <SPAN style="color:#00007F">If</SPAN> Target.Locked = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        ActiveSheet.Unprotect<br>    <SPAN style="color:#00007F">If</SPAN> MsgBox("Unlock Cell?", vbYesNo, "?????") = vbYes <SPAN style="color:#00007F">Then</SPAN> Target.Locked = <SPAN style="color:#00007F">False</SPAN><br>        ActiveSheet.Protect<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
I think this may be better suited:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_SelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> Pword <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> changed <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> changed = Intersect(Target, Range("V5:V2004"))<br>    <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> changed <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>    <br>        <SPAN style="color:#00007F">If</SPAN> Target.Locked = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <br>            <SPAN style="color:#00007F">If</SPAN> MsgBox("Unlock Cell?", vbYesNo, "?????") = vbYes <SPAN style="color:#00007F">Then</SPAN><br>                Pword = InputBox("Enter Password", "BubbleGum")<br>                <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> Getout<br>                ActiveSheet.Unprotect Pword<br>            Target.Locked = <SPAN style="color:#00007F">False</SPAN><br>                <br>                ActiveSheet.Protect Pword<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>Getout: MsgBox "Wrong Password", vbCritical, "Your Busted"<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
I think this may be better suited:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Pword As String
Dim changed As Range

Set changed = Intersect(Target, Range("V5:V2004"))

If Not changed Is Nothing Then

If Target.Locked = True Then

If MsgBox("Unlock Cell?", vbYesNo, "?????") = vbYes Then
Pword = InputBox("Enter Password", "BubbleGum")
On Error GoTo Getout
ActiveSheet.Unprotect Pword
Target.Locked = False

ActiveSheet.Protect Pword
End If

End If

End If
Exit Sub
Getout: MsgBox "Wrong Password", vbCritical, "Your Busted"

End Sub

Hmmm doesn't work. Is there anything I must edit or change to the code you provided?

I added it in, and made sure that all cells are 'unlocked' before applying the code. I also tried to put a password to the sheet. However V5:V2004 can still be edited without restriction!
 
Upvote 0
I thought the code you posted worked for locking the cells??

Here is what I was using in the sheet module:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br><SPAN style="color:#007F00">'    '   Code goes in the Worksheet specific module</SPAN><br><SPAN style="color:#007F00">'    Dim rng As Range</SPAN><br><SPAN style="color:#007F00">'        '   Set Target Range, i.e. Range("A1, B2, C3"), or Range("A1:B3")</SPAN><br><SPAN style="color:#007F00">'        Set rng = Target.Parent.Range("V5:V2004")</SPAN><br><SPAN style="color:#007F00">'             '   Only look at single cell changes</SPAN><br><SPAN style="color:#007F00">'            If Target.Count > 1 Then Exit Sub</SPAN><br><SPAN style="color:#007F00">'            '   Only look at that range</SPAN><br><SPAN style="color:#007F00">'            If Intersect(Target, rng) Is Nothing Then Exit Sub</SPAN><br><SPAN style="color:#007F00">'            '   Action if Condition(s) are met (do your thing here)</SPAN><br><SPAN style="color:#007F00">'            ActiveSheet.Unprotect "manfield"</SPAN><br><SPAN style="color:#007F00">'                Target.Locked = True</SPAN><br><SPAN style="color:#007F00">'            ActiveSheet.Protect "PasswordGoesHere"</SPAN><br><br><SPAN style="color:#00007F">Dim</SPAN> changed <SPAN style="color:#00007F">As</SPAN> Range<br><br><SPAN style="color:#00007F">Set</SPAN> changed = Intersect(Target, Range("V5:V2004"))<br><br><br><br><SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> changed <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> Target.Locked <> <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <br>       <br>        Target.Locked = <SPAN style="color:#00007F">True</SPAN><br>       <br>    <SPAN style="color:#00007F">Else</SPAN><br>        <br>        <br>        <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_SelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> Pword <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> changed <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> changed = Intersect(Target, Range("V5:V2004"))<br>    <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> changed <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>    <br>        <SPAN style="color:#00007F">If</SPAN> Target.Locked = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <br>            <SPAN style="color:#00007F">If</SPAN> MsgBox("Unlock Cell?", vbYesNo, "?????") = vbYes <SPAN style="color:#00007F">Then</SPAN><br>                Pword = InputBox("Enter Password", "BubbleGum")<br>                <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> Getout<br>                ActiveSheet.Unprotect Pword<br>            Target.Locked = <SPAN style="color:#00007F">False</SPAN><br>                <br>                ActiveSheet.Protect Pword<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>Getout: MsgBox "Wrong Password", vbCritical, "Your Busted"<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Code:
[FONT=Courier][COLOR=#00007f]Private[/COLOR] [COLOR=#00007f]Sub[/COLOR] Worksheet_Change([COLOR=#00007f]ByVal[/COLOR] Target [COLOR=#00007f]As[/COLOR] Range)
[COLOR=#007f00]'    '   Code goes in the Worksheet specific module[/COLOR]
[COLOR=#007f00]'    Dim rng As Range[/COLOR]
[COLOR=#007f00]'        '   Set Target Range, i.e. Range("A1, B2, C3"), or Range("A1:B3")[/COLOR]
[COLOR=#007f00]'        Set rng = Target.Parent.Range("V5:V2004")[/COLOR]
[COLOR=#007f00]'             '   Only look at single cell changes[/COLOR]
[COLOR=#007f00]'            If Target.Count > 1 Then Exit Sub[/COLOR]
[COLOR=#007f00]'            '   Only look at that range[/COLOR]
[COLOR=#007f00]'            If Intersect(Target, rng) Is Nothing Then Exit Sub[/COLOR]
[COLOR=#007f00]'            '   Action if Condition(s) are met (do your thing here)[/COLOR]
[COLOR=#007f00]'            ActiveSheet.Unprotect "manfield"[/COLOR]
[COLOR=#007f00]'                Target.Locked = True[/COLOR]
[COLOR=#007f00]'            ActiveSheet.Protect "PasswordGoesHere"[/COLOR]

[COLOR=#00007f]Dim[/COLOR] changed [COLOR=#00007f]As[/COLOR] Range

[COLOR=#00007f]Set[/COLOR] changed = Intersect(Target, Range("V5:V2004"))



[COLOR=#00007f]If[/COLOR] [COLOR=#00007f]Not[/COLOR] changed [COLOR=#00007f]Is[/COLOR] [COLOR=#00007f]Nothing[/COLOR] [COLOR=#00007f]Then[/COLOR]
    [COLOR=#00007f]If[/COLOR] Target.Locked <> [COLOR=#00007f]True[/COLOR] [COLOR=#00007f]Then[/COLOR]
        
       
        Target.Locked = [COLOR=#00007f]True[/COLOR]
       
    [COLOR=#00007f]Else[/COLOR]
        
        
        
    [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]If[/COLOR]
[COLOR=#00007f]End[/COLOR] [COLOR=#00007f]If[/COLOR]

[COLOR=#00007f]End[/COLOR] [COLOR=#00007f]Sub[/COLOR]

[COLOR=#00007f]Private[/COLOR] [COLOR=#00007f]Sub[/COLOR] Worksheet_SelectionChange([COLOR=#00007f]ByVal[/COLOR] Target [COLOR=#00007f]As[/COLOR] Range)
    [COLOR=#00007f]Dim[/COLOR] Pword [COLOR=#00007f]As[/COLOR] [COLOR=#00007f]String[/COLOR]
    [COLOR=#00007f]Dim[/COLOR] changed [COLOR=#00007f]As[/COLOR] Range
    
    [COLOR=#00007f]Set[/COLOR] changed = Intersect(Target, Range("V5:V2004"))
    
    [COLOR=#00007f]If[/COLOR] [COLOR=#00007f]Not[/COLOR] changed [COLOR=#00007f]Is[/COLOR] [COLOR=#00007f]Nothing[/COLOR] [COLOR=#00007f]Then[/COLOR]
    
        [COLOR=#00007f]If[/COLOR] Target.Locked = [COLOR=#00007f]True[/COLOR] [COLOR=#00007f]Then[/COLOR]
            
            [COLOR=#00007f]If[/COLOR] MsgBox("Unlock Cell?", vbYesNo, "?????") = vbYes [COLOR=#00007f]Then[/COLOR]
                Pword = InputBox("Enter Password", "BubbleGum")
                [COLOR=#00007f]On[/COLOR] [COLOR=#00007f]Error[/COLOR] [COLOR=#00007f]GoTo[/COLOR] Getout
                ActiveSheet.Unprotect Pword
            Target.Locked = [COLOR=#00007f]False[/COLOR]
                
                ActiveSheet.Protect Pword
            [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]If[/COLOR]
            
        [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]If[/COLOR]
        
    [COLOR=#00007f]End[/COLOR] [COLOR=#00007f]If[/COLOR]
    [COLOR=#00007f]Exit[/COLOR] [COLOR=#00007f]Sub[/COLOR]
Getout: MsgBox "Wrong Password", vbCritical, "Your Busted"
    
[COLOR=#00007f]End[/COLOR] [COLOR=#00007f]Sub[/COLOR][/FONT]
Hi there, this is the code that you provided. I changed all passwords to 'Manfield'. Applied the code.

Here's what happened:
When I click on cell V5 and type some data inside, the cell becomes locked after I move on to cell V6. When I click back on cell V5, this message pops up,

Unlock Cells? Yes or No

Upon clicking Yes, it prompts for a password, and I keyed in Manfield.
The password window disappear and I proceed to re enter or edit the cell V5. However upon clicking enter, an error appears.

Run-time error '1004'
Unable to set the Locked property of the Range class.


I clicked Debug and this line is highlighted

Target.Locked = True
 
Upvote 0
Hmm?

maybe the addition of protection would do...

<font face=Courier New>       ActiveSheet.Unprotect ("")<br>       Target.Locked = <SPAN style="color:#00007F">True</SPAN><br>       ActiveSheet.Protect ("")</FONT>
 
Upvote 0
Don't forget to add your password into the (""). :)

Grrrr, still the same problem.

Do you mind linking all the parts for me? I am a total amateur at this and I wouldn't know where to add in passwords or whatever if you didn't say... My apologies for troubling you.
 
Upvote 0
No trouble.
However if this doesn't get it I may Log off for the night.
zzzzzzz.


<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br><br><SPAN style="color:#00007F">Dim</SPAN> changed <SPAN style="color:#00007F">As</SPAN> Range<br><br><SPAN style="color:#00007F">Set</SPAN> changed = Intersect(Target, Range("V5:V2004"))<br><br><br><br><SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> changed <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> TargetLocked <> <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>       <br>       ActiveSheet.Unprotect ("Manfeild")<br>       Target.Locked = <SPAN style="color:#00007F">True</SPAN><br>       ActiveSheet.Protect ("Manfeild")<br>       <br>    <SPAN style="color:#00007F">Else</SPAN><br>        <br>        <br>        <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_SelectionChange(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> Pword <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> changed <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> changed = Intersect(Target, Range("V5:V2004"))<br>    <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> changed <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>    <br>        <SPAN style="color:#00007F">If</SPAN> Target.Locked = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            <br>            <SPAN style="color:#00007F">If</SPAN> MsgBox("Unlock Cell?", vbYesNo, "?????") = vbYes <SPAN style="color:#00007F">Then</SPAN><br>                Pword = InputBox("Enter Password", "BubbleGum")<br>                <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> Getout<br>                ActiveSheet.Unprotect Pword<br>            Target.Locked = <SPAN style="color:#00007F">False</SPAN><br>                <br>                ActiveSheet.Protect Pword<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>Getout: MsgBox "Wrong Password", vbCritical, "Your Busted"<br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

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