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.
 
Ah, the nightmare of merged cells strikes again!
Avoid using them at all costs!!! They wreak havoc like this to your VBA code and other Excel tasks (such as sorting).

You can usually get the same visual effect as using merged cells without all the headaches if you use the Center Across Selection formatting option instead.
See: https://www.atlaspm.com/toms-tutori...er-across-selection-instead-of-merging-cells/
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Ah, the nightmare of merged cells strikes again!
Avoid using them at all costs!!! They wreak havoc like this to your VBA code and other Excel tasks (such as sorting).

You can usually get the same visual effect as using merged cells without all the headaches if you use the Center Across Selection formatting option instead.
See: https://www.atlaspm.com/toms-tutori...er-across-selection-instead-of-merging-cells/

Thank you so much. After my light bulb moment about the merged cells I recreated the spreadsheet. It is working beautifully!!
 
Upvote 0
Glad to hear the mystery is solved!:)
 
Upvote 0
As previously stated merged cells should be avoided but I was wondering if the code could be make to work with merged cells and this seems to work


Change the line to
Code:
 xRg.MergeArea.Locked = True

But really merged cells should be avoided.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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