Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 295
Default Locking/Unlocking cells upon condition

I have put the following code into the worksheet code but it doesn't work.
Can someone put me straight please?

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value < 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1:add.prv.yrs.2").Locke d = False
ActiveSheet.Protect
End If
End Sub

Thanks, Brett

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default Locking/Unlocking cells upon condition

Go back and create another named range that includes the cell(s) now referred
to as add.prv.1 and add.priv.yrs.2 and then revise your .Locked statement.

Lets say that add.prv.1 refers to A1 and add.priv.yrs.2 refers to B1. You
could select A1 and B1 and give them a name like add.priv.yrs.all then use
ActiveSheet.Range("add.priv.yrs.all").Locked = False

An alternative is to break the statement into two statements:
ActiveSheet.Range("add.priv.yrs.1").Locked = False
ActiveSheet.Range("add.priv.yrs.2").Locked = False




"Brettjg" wrote:

I have put the following code into the worksheet code but it doesn't work.
Can someone put me straight please?

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value < 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1:add.prv.yrs.2").Locke d = False
ActiveSheet.Protect
End If
End Sub

Thanks, Brett

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 295
Default Locking/Unlocking cells upon condition

Hi JL, I've changed the code to this, but it still doesn't work. This is the
only code in the sheet code, nothing else at all. Am I missing some other
code that it needs? Currently the value of "add.years.1" is 1 so the cells
should be unlocked, but they are still locked.

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value = 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1").Locked = True
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.2").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
ActiveSheet.Protect
End If
End Sub


"JLatham" wrote:

Go back and create another named range that includes the cell(s) now referred
to as add.prv.1 and add.priv.yrs.2 and then revise your .Locked statement.

Lets say that add.prv.1 refers to A1 and add.priv.yrs.2 refers to B1. You
could select A1 and B1 and give them a name like add.priv.yrs.all then use
ActiveSheet.Range("add.priv.yrs.all").Locked = False

An alternative is to break the statement into two statements:
ActiveSheet.Range("add.priv.yrs.1").Locked = False
ActiveSheet.Range("add.priv.yrs.2").Locked = False




"Brettjg" wrote:

I have put the following code into the worksheet code but it doesn't work.
Can someone put me straight please?

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value < 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1:add.prv.yrs.2").Locke d = False
ActiveSheet.Protect
End If
End Sub

Thanks, Brett

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,365
Default Locking/Unlocking cells upon condition

You say "sheet code" - so I am presuming you want this to work automatically
and detect when add.years.1 goes = 3

Then in the sheet's code you need to use the _Calculate() event processor to
do this for you most likely, and I am also presuming you may want to toggle
the .Locked state of those cells, so here's what code to work that way could
look like:

Private Sub Worksheet_Calculate()
ActiveSheet.Unprotect
If Range("add.years.1").Value < 3 Then
ActiveSheet.Range("add.prv.yrs.1").Locked = False
ActiveSheet.Range("add.prv.yrs.2").Locked = False
Else
'= 3 processing
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
End If
ActiveSheet.Protect
End Sub

If your worksheet is protected with a password, then you'll need to provide
the password in the code, similiar to this:

Private Sub Worksheet_Calculate()
ActiveSheet.Unprotect Password:="mypassword"
If Range("add.years.1").Value < 3 Then
ActiveSheet.Range("add.prv.yrs.1").Locked = False
ActiveSheet.Range("add.prv.yrs.2").Locked = False
Else
'= 3 processing
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
End If
ActiveSheet.Protect Password:="mypassword"
End Sub

"Brettjg" wrote:

Hi JL, I've changed the code to this, but it still doesn't work. This is the
only code in the sheet code, nothing else at all. Am I missing some other
code that it needs? Currently the value of "add.years.1" is 1 so the cells
should be unlocked, but they are still locked.

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value = 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1").Locked = True
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.2").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
ActiveSheet.Protect
End If
End Sub


"JLatham" wrote:

Go back and create another named range that includes the cell(s) now referred
to as add.prv.1 and add.priv.yrs.2 and then revise your .Locked statement.

Lets say that add.prv.1 refers to A1 and add.priv.yrs.2 refers to B1. You
could select A1 and B1 and give them a name like add.priv.yrs.all then use
ActiveSheet.Range("add.priv.yrs.all").Locked = False

An alternative is to break the statement into two statements:
ActiveSheet.Range("add.priv.yrs.1").Locked = False
ActiveSheet.Range("add.priv.yrs.2").Locked = False




"Brettjg" wrote:

I have put the following code into the worksheet code but it doesn't work.
Can someone put me straight please?

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value < 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1:add.prv.yrs.2").Locke d = False
ActiveSheet.Protect
End If
End Sub

Thanks, Brett

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 295
Default Locking/Unlocking cells upon condition

Crackajack JL, it works beautifully. Thank you so much for your help. I also
tried another If after the first one for a different test and range and that
was successful as well. Do I presume that anything I want to do like this has
to be in the same Private Sub which must be called Worksheet_Calculate?

"JLatham" wrote:

You say "sheet code" - so I am presuming you want this to work automatically
and detect when add.years.1 goes = 3

Then in the sheet's code you need to use the _Calculate() event processor to
do this for you most likely, and I am also presuming you may want to toggle
the .Locked state of those cells, so here's what code to work that way could
look like:

Private Sub Worksheet_Calculate()
ActiveSheet.Unprotect
If Range("add.years.1").Value < 3 Then
ActiveSheet.Range("add.prv.yrs.1").Locked = False
ActiveSheet.Range("add.prv.yrs.2").Locked = False
Else
'= 3 processing
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
End If
ActiveSheet.Protect
End Sub

If your worksheet is protected with a password, then you'll need to provide
the password in the code, similiar to this:

Private Sub Worksheet_Calculate()
ActiveSheet.Unprotect Password:="mypassword"
If Range("add.years.1").Value < 3 Then
ActiveSheet.Range("add.prv.yrs.1").Locked = False
ActiveSheet.Range("add.prv.yrs.2").Locked = False
Else
'= 3 processing
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
End If
ActiveSheet.Protect Password:="mypassword"
End Sub

"Brettjg" wrote:

Hi JL, I've changed the code to this, but it still doesn't work. This is the
only code in the sheet code, nothing else at all. Am I missing some other
code that it needs? Currently the value of "add.years.1" is 1 so the cells
should be unlocked, but they are still locked.

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value = 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1").Locked = True
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.2").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
ActiveSheet.Protect
End If
End Sub


"JLatham" wrote:

Go back and create another named range that includes the cell(s) now referred
to as add.prv.1 and add.priv.yrs.2 and then revise your .Locked statement.

Lets say that add.prv.1 refers to A1 and add.priv.yrs.2 refers to B1. You
could select A1 and B1 and give them a name like add.priv.yrs.all then use
ActiveSheet.Range("add.priv.yrs.all").Locked = False

An alternative is to break the statement into two statements:
ActiveSheet.Range("add.priv.yrs.1").Locked = False
ActiveSheet.Range("add.priv.yrs.2").Locked = False




"Brettjg" wrote:

I have put the following code into the worksheet code but it doesn't work.
Can someone put me straight please?

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value < 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1:add.prv.yrs.2").Locke d = False
ActiveSheet.Protect
End If
End Sub

Thanks, Brett



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 22,906
Default Locking/Unlocking cells upon condition

You can have multiple event handlers in a sheet module, but not multiples of one
type of event.

i.e. one Worksheet_Calculate event per sheet.

That one event could call various macros or move to statements based on the
value of a cell or cells. Or just expand upon the conditions in Jerry's code.

e.g. using Select Case method to choose what to do when a condition is met with
your Worksheet_Calculate event.

Example of Select Case code..........

Private Sub Worksheet_Calculate()
Dim Target As Range
For Each Target In Me.Range("A1:A100")
With Target
Select Case .Value
Case Is = 3: mymacro3
Case Is = 1: mymacro1
Case Is = 2: mymacro2
Case Is = 4: mymacro4
'etc.
End Select
End With
Next Target
End Sub


Gord Dibben MS Excel MVP
On Sat, 10 Mar 2007 11:57:07 -0800, Brettjg
wrote:

Crackajack JL, it works beautifully. Thank you so much for your help. I also
tried another If after the first one for a different test and range and that
was successful as well. Do I presume that anything I want to do like this has
to be in the same Private Sub which must be called Worksheet_Calculate?

"JLatham" wrote:

You say "sheet code" - so I am presuming you want this to work automatically
and detect when add.years.1 goes = 3

Then in the sheet's code you need to use the _Calculate() event processor to
do this for you most likely, and I am also presuming you may want to toggle
the .Locked state of those cells, so here's what code to work that way could
look like:

Private Sub Worksheet_Calculate()
ActiveSheet.Unprotect
If Range("add.years.1").Value < 3 Then
ActiveSheet.Range("add.prv.yrs.1").Locked = False
ActiveSheet.Range("add.prv.yrs.2").Locked = False
Else
'= 3 processing
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
End If
ActiveSheet.Protect
End Sub

If your worksheet is protected with a password, then you'll need to provide
the password in the code, similiar to this:

Private Sub Worksheet_Calculate()
ActiveSheet.Unprotect Password:="mypassword"
If Range("add.years.1").Value < 3 Then
ActiveSheet.Range("add.prv.yrs.1").Locked = False
ActiveSheet.Range("add.prv.yrs.2").Locked = False
Else
'= 3 processing
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
End If
ActiveSheet.Protect Password:="mypassword"
End Sub

"Brettjg" wrote:

Hi JL, I've changed the code to this, but it still doesn't work. This is the
only code in the sheet code, nothing else at all. Am I missing some other
code that it needs? Currently the value of "add.years.1" is 1 so the cells
should be unlocked, but they are still locked.

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value = 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1").Locked = True
ActiveSheet.Range("add.prv.yrs.1").Locked = True
ActiveSheet.Range("add.prv.2").Locked = True
ActiveSheet.Range("add.prv.yrs.2").Locked = True
ActiveSheet.Protect
End If
End Sub


"JLatham" wrote:

Go back and create another named range that includes the cell(s) now referred
to as add.prv.1 and add.priv.yrs.2 and then revise your .Locked statement.

Lets say that add.prv.1 refers to A1 and add.priv.yrs.2 refers to B1. You
could select A1 and B1 and give them a name like add.priv.yrs.all then use
ActiveSheet.Range("add.priv.yrs.all").Locked = False

An alternative is to break the statement into two statements:
ActiveSheet.Range("add.priv.yrs.1").Locked = False
ActiveSheet.Range("add.priv.yrs.2").Locked = False




"Brettjg" wrote:

I have put the following code into the worksheet code but it doesn't work.
Can someone put me straight please?

Private Sub PERSONAL_LOCK()
If Range("add.years.1").Value < 3 Then
ActiveSheet.UNPROTECT
ActiveSheet.Range("add.prv.1:add.prv.yrs.2").Locke d = False
ActiveSheet.Protect
End If
End Sub

Thanks, Brett


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unlocking protected cells Vanilla Skies Excel Worksheet Functions 0 September 26th 06 04:04 AM
Unlocking cells Vanilla Skies Excel Worksheet Functions 1 September 25th 06 04:30 AM
locking formula in cells in without locking whole sheet SuziQ Excel Discussion (Misc queries) 1 July 21st 06 03:58 PM
conditional unlocking of cells hiryuu Excel Worksheet Functions 1 October 19th 05 12:44 PM
Unlocking Cells when a worksheet is protected... racmb1975 Excel Discussion (Misc queries) 2 May 3rd 05 08:57 PM


All times are GMT +1. The time now is 08:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"