Data not protected when selection changes

crazyoldcat

New Member
Joined
Aug 4, 2009
Messages
19
Hello,

I am working on a spreadsheet with VBA and am trying to make sure the cells are locked AFTER data is entered. What is happening now is that I can go in and edit the data as much as I want to but the moment I delete or clear the data the cell locks. This is quite the opposite of what I wanted to accomplish. I want the cells to stay unlocked until there is data in the cells then I want the cells with data in them locked and the data protected.
This is the script I am using.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
On Error Resume Next
Set xRg = Intersect(Range("A5:w228"), Target)
If xRg Is Nothing Then Exit Sub
Target.Worksheet.Unprotect Password:="password"
xRg.Locked = True
Target.Worksheet.Protect Password:="password"
End Sub

Thank you in advance for your help.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim xRg As Range

    On Error Resume Next
    
    Set xRg = Intersect(Range("A5:w228"), Target)
    
    If xRg Is Nothing Then Exit Sub
    
    [COLOR=#ff0000]ActiveSheet[/COLOR].Unprotect Password:="password"
    xRg.Locked = True
    [COLOR=#ff0000]ActiveSheet[/COLOR].Protect Password:="password"
    
End Sub
And note that you need to make sure that your cells are all unlocked initially. By default, Excel cells are all locked and the sheet is unprotected.
So you need to make sure that you have change them to be unlocked for your code to work properly.
 
Upvote 0
Hi,

try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Target.Parent.Unprotect
Target.Locked = true
Target.Parent.Protect

Don't set a password, it is meaningless.

regards
 
Upvote 0
Don't set a password, it is meaningless.
Not if you are using a strong password (sometimes people just simplify it here for posting purposes, as they do not really want to post the real password).
Otherwise, users can easily unprotect it and bypass the measures you are trying to enforce.
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim xRg As Range

    On Error Resume Next
    
    Set xRg = Intersect(Range("A5:w228"), Target)
    
    If xRg Is Nothing Then Exit Sub
    
    [COLOR=#ff0000]ActiveSheet[/COLOR].Unprotect Password:="password"
    xRg.Locked = True
    [COLOR=#ff0000]ActiveSheet[/COLOR].Protect Password:="password"
    
End Sub
And note that you need to make sure that your cells are all unlocked initially. By default, Excel cells are all locked and the sheet is unprotected.
So you need to make sure that you have change them to be unlocked for your code to work properly.

I have tried both methods listed, thank you by the way, and IO am still not getting the result I am looking for. Is it possible to send someone the spreadsheet to see if they can figure out what I am doing wrong?
 
Upvote 0
You can put the file on dropbox or similar service and post a link here.
 
Upvote 0
I cannot download files from my current location (security policy).

Have you manually gone in and unlocked all cells to start?
Walk us through an example which is not working for you and tell us exactly what is happening.
 
Upvote 0
I have unlocked all cells to start by selecting all and then "format cells" I then click on the protection tab and uncheck "locked". I click on "OK" and then save the changes. When I add the VBA script I save it as a macro enabled file then protect the sheet. At this point everything seems to be going as designed. I enter data in a cell and tab or enter to change cells. the sheet changes to unprotect and then back to protect right away. The part that is not working is that the cells are not locking/protected once there is data in the cells. I can go back in and write over the data and even delete the data. When the data in those cells is deleted THEN the cells are locked and I have to unprotect the sheet to unlock them.

Thanks in advance for your help.
 
Upvote 0
It seems to work just fine for me.

What cell, exactly are you entering your data in?
Note that you have written your code so that it only applies to the range A5:W228.
So anything entered outside of that range will not be affected.
 
Upvote 0
The original code you posted is working for me. If I type something in A5 then try to over wright or delete I get the protected cell message.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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