Multiple If Statement to Lock Group of Cells

NonniKcaM

New Member
Joined
Dec 4, 2012
Messages
8
Hi there,

Wonder if anyone can help me with this issue. I've written a piece of code to lock cells AND unlock cells on a condition. For example if D5 = 1, users will only be able to input into the first group of cells (when protected) so the rest will be locked. This is purely to allow users to tab through everything easily. Originally the code worked when it only locked cells but I then encountered a problem where once a cell was locked it would not unlock if another condition was now met,

Hope this made some sense...

Thanks a lot :)

Nonn

Code bellow in short form (there is actually 19 conditions but only showing first 3):

Private Sub Lock_Change(ByVal Target As Range)

If [D5] = 1 Then
ActiveSheet.Unprotect ("password")
[D7:V46].Locked = True And [C7:C46].Locked = False
ActiveSheet.Protect ("password")

ElseIf [D5] = 2 Then
ActiveSheet.Unprotect ("password")
[E7:V46].Locked = True And [C7:D46].Locked = False
ActiveSheet.Protect ("password")

ElseIf [D5] = 3 Then
ActiveSheet.Unprotect ("password")
[F7:V46].Locked = True And [C7:E46].Locked = False
ActiveSheet.Protect ("password")

Else
ActiveSheet.Unprotect ("password")
[D7:V46].Locked = False
ActiveSheet.Protect ("password")

End If
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You can't do this:

Code:
[D7:V46].Locked = True And [C7:C46].Locked = False

And is a logical operator, not a word to join two statements. Try:

Code:
[D7:V46].Locked = True
[C7:C46].Locked = False
 
Upvote 0
Thanks for your reply Andrew. That was a rather silly mistake, but I'm afraid it has not completely fixed the code. Can you or anyone else see any other errors? Also would it be beneficially to use a loop to call this statement so it constantly checks to see if its been changed?

Nonn
 
Upvote 0
Meaning that the code still doesn't give the desired result (locking the cells when the condition is met)
 
Upvote 0
Updated code looks like this:

Private Sub Lock_Change(ByVal Target As Range)

If [D5] = 1 Then
ActiveSheet.Unprotect ("password")
[D7:V46].Locked = True
[C7:C46].Locked = False
ActiveSheet.Protect ("password")

ElseIf [D5] = 2 Then
ActiveSheet.Unprotect ("password")
[E7:V46].Locked = True
[C7:D46].Locked = False
ActiveSheet.Protect ("password")

ElseIf [D5] = 3 Then
ActiveSheet.Unprotect ("password")
[F7:V46].Locked = True
[C7:E46].Locked = False
ActiveSheet.Protect ("password")

Else
ActiveSheet.Unprotect ("password")
[C7:V46].Locked = False
ActiveSheet.Protect ("password")

End If

End Sub

Nonn
 
Upvote 0
And in what way isn't that working? It looks like the first line should be:

Private Sub Worksheet_Change(ByVal Target As Range)
 
Upvote 0
Thanks Andrew that fixed it! Never programmed using Visual Basics before so making a lot of amateur errors. Cheers for your time and have a good day
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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