Locking specific cell

OKearney

New Member
Joined
Jan 4, 2016
Messages
11
I have already asked a similar question but I am still not getting the results I had hope for. My goal is to have for instance an entire worksheet that is locked so that other users cannot change anything. However, there will be an exception of cells like C6, C12, C18, etc.. In these particular cells there will be a drop down tab where users can select either "red" or "blue". I want those specific cells to be unlocked UNTIL a user selects one of the options and then it has to lock. So basically it will only lock once either red or blue is selected. I have been playing around with it but I can only get it to the point where once any change is made the entire workbook locks, which defeats the purpose. This is what I had written so far which isn't working:
Private Sub Worksheet_Change(ByVal Target As Range) Dim cel As Range
ActiveSheet.Unprotect ' Password:="secret"
For Each cel In Target
If cel.Value = "Red" Or cel.Value = "Blue" Then
cel.Locked = True
Else
cel.Locked = False
End If
Next cel
ActiveSheet.Protect ' Password:= "secret"
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.
just fyi, you can also write it this way...

Code:
cel.Locked = [/COLOR][COLOR=#333333]cel.Value = "Red" Or cel.Value = "Blue"[/COLOR]
 
Upvote 0
ok i need to remember not to use code tags on here, they dont work properly...


cel.Locked = cel.Value = "Red" Or cel.Value = "Blue"
 
Upvote 0
How would excel know to make this work only for this specific cell if all other cells are already locked?
 
Upvote 0
you check if it intersects with the specific cell...

If Not Intersect(Target, Range("A1")) is Nothing Then
'Target includes cell A1
End If

or just check the address directly...

If Target.Address = "$A$1" Then
'Target is cell A1
End If

the 2nd way i think is better because the first one will trigger true if more than one cell is selected
 
Upvote 0
not to mention locked cells with the sheet protected can't be edited so they won't call your event regardless... only unlocked cells can call a change event when the sheet is protected
 
Last edited:
Upvote 0
Ok sorry I am a complete beginner when it comes to excel and VBA so I need a bit of paint by numbers here. Once I get into the workspace to create a macro what is it exactly that I need to write? So far I have:

Sub Lockcell()
cel.Locked = cel.Value = "red" Or cel.Value = "blue"
End Sub

So where would I add in that other part? (Again... completely new to all of this so it is like a foreign language)
 
Upvote 0
try this...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    'since your sheet is locked only unlocked cells will call this
    For Each cell In Target
    
        If cell.Value = "Red" Or cell.Value = "Blue" Then
            ActiveSheet.Unprotect ' Password:="secret"
        
            cell.Locked = True
        
            ActiveSheet.Protect ' Password:= "secret"
        End If
    
    Next cell
    
End Sub

since unlocked cells only can call this, you only need to decide wheteher or not to lock, so i take no action if the value is not red or blue

just add this to your worksheet code
 
Last edited:
Upvote 0
I just tested and seemed to work ok, this is what I did...

1. Opened a blank workbook
2. Unlocked cell A1
3. Then I protected the sheet
4. Opend VBA Editor and pasted the code above into the worksheet code
5. Then I typed Red into A1 (this called the macro and locked the cell)
6. Then I tried to type Blue into A1 and I got the warning that the cell was locked and couldn't be edited.

I am guessing you pasted the change event into a module and not the worksheet code
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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