Protect Individual Cells by Formula?

golf4

Active Member
Joined
Jul 8, 2002
Messages
452
Hi, everyone -

I just had a quick query. Working on my income calculation tool, I ran into something I haven't tried before. First, my data entry sheet is fully protected, via password, except for cells I use to enter specific data for calculation.

What I'm wondering is what the best method is to accomplish the following (example): if cell A1 equals either 4 or 5, then how would I protect cell A2? :oops: I'm thinking it's got to be something like =IF(OR(A1=4,A1=5),PROTECT(A2),"")? Or could the same thing be accomplished in Conditional Formatting? Any help would be great.

Thanks,

Golf
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You don't protect cells based on formulas. Cells that contain formulas can be protected, tho, and will still calculate the formulas, but they're saved from being overwritten.

If that doesn't help, why dontcha tell us exactly what you're trying to do so we can help better?

:)
 
Upvote 0
See if this helps. Right click on your sheet tab, left click on View Code, and paste the following procedure into the large white area that is the worksheet module. Press Alt+Q to return to the worksheet.

Modify for password.


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Or Target.Cells.Count > 1 Then Exit Sub
If Target.Value = 4 Or Target.Value = 5 Then
ActiveSheet.Unprotect ("YourPassword")
Range("A2").Locked = True
ActiveSheet.Protect ("YourPassword")
Else
ActiveSheet.Unprotect ("YourPassword")
Range("A2").Locked = False
ActiveSheet.Protect ("YourPassword")
End If
End Sub
 
Upvote 0
Hi, Anne and Tom -

Thanks for the responses. I really appreciate it. Just a little more clarification on my question. I've included the actual cell references:

1. My Data Entry Sheet is password-protected except for a select number of data entry cells like H36;

2. Basically, what I want to happen is first to look at the MIN of cells E40 (bedroom size) and H42 (Voucher size);

3. If the MIN of E40 and H42 is either 4 or 5, then I was looking to have cell H36 locked off - otherwise leave it untouched;

Anne - I was originally looking to insert the "locking" formula in an adjacent cell to H36 (I36) and then hide and protect I36 so it can't be overwritten.

Tom - Is there a way to modify your VB code to accomplish this? Would I still have to unprotect the whole sheet if H36 is already unprotected?


Thanks to both of you for your help,

Golf
 
Upvote 0
Better stick with Tom, Golf. A wee bit over my head!
:)
 
Upvote 0
Replace the first procedure I posted with this one in the sheet module, and see if it gets you closer to what you really need.

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("E40,H42")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
If Application.Min(Range("E40,H42")) = 4 Or Application.Min(Range("E40,H42")) = 5 Then
ActiveSheet.Unprotect ("YourPassword")
Range("H36").Locked = True
ActiveSheet.Protect ("YourPassword")
Else
ActiveSheet.Unprotect ("YourPassword")
Range("H36").Locked = False
ActiveSheet.Protect ("YourPassword")
End If
End Sub
 
Upvote 0
Thanks for the help, Tom -
Your suggested code works perfectly!!!! (y)

Thanks again and take care,

Golf
 
Upvote 0
Hi, Tom -
One additional question: I attempted to modify your code to lock off one additional cell (cel H37 as bolded below), but the code seems to be locking only cell H36. Am I doing something wrong in modifying it in this manner?

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("E40,H42")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
If Application.Min(Range("E40,H42")) = 4 Or Application.Min(Range("E40,H42")) = 5 Then
ActiveSheet.Unprotect ("led52not")
Range("H36").Locked = True
Range("H37").Locked = True
ActiveSheet.Protect ("led52not")
Else
ActiveSheet.Unprotect ("led52not")
Range("H36").Locked = False
Range("H37").Locked = False
ActiveSheet.Protect ("led52not")
End If
End Sub

Thanks for the help,
Golf
 
Upvote 0
Hey Tom -

OK, IT'S LATE!!!!! :rolleyes:

I went ahead and saved the tool and now all of the cells I need locked off works perfectly. Again, thanks for the help......

Golf
 
Upvote 0
Not a big deal, but FYI you can combine those two code lines of yours into one line, two different ways in your case, for more efficient code:

You have:
Range("H36").Locked = True
Range("H37").Locked = True


Can be either
Range("H36:H37").Locked = True 'or False
because they are contiguous cells

or

Range("H36,H37").Locked = True 'or False
because they are separate cells.

In any case it looks like you have what you need.
 
Upvote 0

Forum statistics

Threads
1,215,736
Messages
6,126,550
Members
449,318
Latest member
Son Raphon

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