Trouble locking cells....

AshN

New Member
Joined
Apr 10, 2012
Messages
4
I am trying to set up an excel workbook that will run something like a time clock. I want certain cells to be locked to all without a password and certain cells to lock once a person has entered data into them. These will all be on the same sheet. I've gotten each to work without the other turned on but as soon as I try to make it all work at the same time it freaks out (i.e. the cells that are supposed to lock after data entry start locking everytime the worksheet is protected, or ask for a password to enter data, etc) It seems like the info I'm using is misreading somewhere. I'm very new to using VBA so I'm having trouble trouble-shooting it. Here's a copy of the VBA I'm using to lock the cells after data entry. If you need more information please let me know. Any help anyone can give would be awesome! Thanks!

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("H4:H30")) Is Nothing Then

If Target.Locked = False Then
ActiveSheet.Unprotect "1234"
Target.Locked = True
ActiveSheet.Protect "1234"
End If
End If
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I haven't tested the code, by looking I feel you missed 'else'

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H4:H30")) Is Nothing Then
If Target.Locked = False Then
ActiveSheet.Unprotect "1234"
Else
Target.Locked = True
ActiveSheet.Protect "1234"
End If
End If
End Sub
 
Upvote 0
I was missing the "else" part but it didn't change anything...now it's unlocking the whole worksheet every time I enter data into the column I want to lock after the data is entered. It's also leaving certain cells in that same column that have no data locked.
 
Upvote 0
Hello, maybe something like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set r = Range("A1:AA100")
If Not Intersect(r, Range("H4:H30")) Is Nothing Then
If r.Locked = False Then
ActiveSheet.Unprotect "1234"
Else
r.Locked = True
ActiveSheet.Protect "1234"
End If
End If
End Sub
 
Upvote 0
Is it possible for me to have some of the cells locked and some set to lock after the data is entered? Do they have to be formatted a certain way in order to have everything read correctly?
 
Upvote 0
Is it possible for me to have some of the cells locked and some set to lock after the data is entered? Do they have to be formatted a certain way in order to have everything read correctly?

Do you want that range (except H1:H30) was locked and since enter data in H1:H30 will lock all sheet?
 
Upvote 0
--Do you want that range (except H1:H30) was locked and since enter data in H1:H30 will lock all sheet?

I specifically want cells A-G locked no matter what and then cell H to lock once data has been entered into it.
 
Upvote 0
Try:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("H4:H30")) Is Nothing Then
Else
ActiveSheet.Unprotect "1234"
Range("H4:H30").Locked = True
ActiveSheet.Protect "1234"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,042
Members
449,063
Latest member
ak94

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