Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default lock a range question

what would be the best way to do this:

if an x is entered in a cell, lock a range of cells in that same row so
nothing in that range could be entered?

--


Gary



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default lock a range question

If you were to start out with all cells on the sheet unlocked then you
could use a worksheet change event similar to this. This checks entry in
columm A and then locks columns C,D and E on that row:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 And Target.Column = 1 _
And Target.Value = "x" Then
Me.Unprotect Password:="mypassword"
Range(Cells(Target.Row, 3), Cells(Target.Row, 5)).Locked = True
Me.Protect Password:="mypassword"
End If
End Sub

Hope this helps
Rowan

Gary Keramidas wrote:
what would be the best way to do this:

if an x is entered in a cell, lock a range of cells in that same row so
nothing in that range could be entered?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default lock a range question

thanks rowan, i had to change the target.count to target ,row.

here is more info.

the sheet is protected, except where users enter data. the verification
column is locked, only the supervisor can enter an x in the verification
column. she unprotects the sheet and verifies the data in v4:z4. if it's ok,
she enters and x in aa4. she does this for rows 4 to potentially, 56.

this same procedure happens in column af4 for range ab4:ae4.

with your code, i adapted it to work in row 4. it locked the range of cells.
so when the supervisor protects the sheet after verifying, the user could
not go back to that line and change anything. don't need any passwords, all
restrictions are through gpo.

so i need to further adapt to for the 2nd range and for rows 4 through 56.

i'll try and see what i can come up with. thanks for the start.

--


Gary


"Rowan Drummond" wrote in message
...
If you were to start out with all cells on the sheet unlocked then you
could use a worksheet change event similar to this. This checks entry in
columm A and then locks columns C,D and E on that row:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 And Target.Column = 1 _
And Target.Value = "x" Then
Me.Unprotect Password:="mypassword"
Range(Cells(Target.Row, 3), Cells(Target.Row, 5)).Locked = True
Me.Protect Password:="mypassword"
End If
End Sub

Hope this helps
Rowan

Gary Keramidas wrote:
what would be the best way to do this:

if an x is entered in a cell, lock a range of cells in that same row so
nothing in that range could be entered?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default lock a range question

rowan:

this seems to work for 1 cell, how do i select columns 11 through 16?

Private Sub Worksheet_Change(ByVal Target As Range)
For x = 4 To 56
If Target.Row = x And Target.Column = 27 _
And Target.Value = "x" Then
Range(Cells(Target.Row, x), Cells(Target.Column,
11)).Locked = True
End If
Next
End Sub

--


Gary


"Rowan Drummond" wrote in message
...
If you were to start out with all cells on the sheet unlocked then you
could use a worksheet change event similar to this. This checks entry in
columm A and then locks columns C,D and E on that row:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 And Target.Column = 1 _
And Target.Value = "x" Then
Me.Unprotect Password:="mypassword"
Range(Cells(Target.Row, 3), Cells(Target.Row, 5)).Locked = True
Me.Protect Password:="mypassword"
End If
End Sub

Hope this helps
Rowan

Gary Keramidas wrote:
what would be the best way to do this:

if an x is entered in a cell, lock a range of cells in that same row so
nothing in that range could be entered?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default lock a range question

Hi Gary

I was about to respond with this:

The target.count is in there to check that only one cell has been
changed before firing the event. This stops the event running if a user
pastes data onto the sheet, selects a range and hits delete etc etc.
Feel free to remove it if you like, otherwise

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
If Not Intersect(Target, Range("AA4:AA56")) Is Nothing _
And Target.Value = "x" Then
Range(Cells(Target.Row, 22), Cells(Target.Row, 26)).Locked = True
ElseIf Not Intersect(Target, Range("AF4:AF56")) Is Nothing _
And Target.Value = "x" Then
Range(Cells(Target.Row, 28), Cells(Target.Row, 31)).Locked = True
End If
End If
End Sub

Does this answer your question?
Regards
Rowan


Gary Keramidas wrote:
rowan:

this seems to work for 1 cell, how do i select columns 11 through 16?

Private Sub Worksheet_Change(ByVal Target As Range)
For x = 4 To 56
If Target.Row = x And Target.Column = 27 _
And Target.Value = "x" Then
Range(Cells(Target.Row, x), Cells(Target.Column,
11)).Locked = True
End If
Next
End Sub



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default lock a range question

rowan:

i think that's it. do you think i need to have a way to unlock the cell if
the x in the verification column is deleted. it most likely won't happen,
but end users have a way of doing the unexplained.


thanks again

--


Gary


"Rowan Drummond" wrote in message
...
Hi Gary

I was about to respond with this:

The target.count is in there to check that only one cell has been changed
before firing the event. This stops the event running if a user pastes
data onto the sheet, selects a range and hits delete etc etc. Feel free to
remove it if you like, otherwise

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
If Not Intersect(Target, Range("AA4:AA56")) Is Nothing _
And Target.Value = "x" Then
Range(Cells(Target.Row, 22), Cells(Target.Row, 26)).Locked = True
ElseIf Not Intersect(Target, Range("AF4:AF56")) Is Nothing _
And Target.Value = "x" Then
Range(Cells(Target.Row, 28), Cells(Target.Row, 31)).Locked = True
End If
End If
End Sub

Does this answer your question?
Regards
Rowan


Gary Keramidas wrote:
rowan:

this seems to work for 1 cell, how do i select columns 11 through 16?

Private Sub Worksheet_Change(ByVal Target As Range)
For x = 4 To 56
If Target.Row = x And Target.Column = 27 _
And Target.Value = "x" Then
Range(Cells(Target.Row, x), Cells(Target.Column,
11)).Locked = True
End If
Next
End Sub



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default lock a range question

As I am sure you know if there is even the remotest possibility of a
user doing something unexpected they are almost certain to do it:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
If Not Intersect(Target, Range("AA4:AA56")) Is Nothing Then
If Target.Value = "x" Then
Range(Cells(Target.Row, 22), Cells(Target.Row, 26)) _
.Locked = True
ElseIf Target.Value = "" Then
Range(Cells(Target.Row, 22), Cells(Target.Row, 26)) _
.Locked = False
End If
ElseIf Not Intersect(Target, Range("AF4:AF56")) Is Nothing Then
If Target.Value = "x" Then
Range(Cells(Target.Row, 28), Cells(Target.Row, 31)) _
.Locked = True
ElseIf Target.Value = "" Then
Range(Cells(Target.Row, 28), Cells(Target.Row, 31)) _
.Locked = False
End If
End If
End If
End Sub

Regards
Rowan

Gary Keramidas wrote:
rowan:

i think that's it. do you think i need to have a way to unlock the cell if
the x in the verification column is deleted. it most likely won't happen,
but end users have a way of doing the unexplained.


thanks again

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default lock a range question

this appears to work, can anyone verify?

Private Sub Worksheet_Change(ByVal Target As Range)
For z = 4 To 56
For y = 11 To 16
If Target.Row = z And Target.Column = 27 _
And Target.Value = "x" Then
Range(Cells(Target.Row, z), Cells(Target.Row, y)).Locked =
True
End If
Next
Next
End Sub


--


Gary


"Rowan Drummond" wrote in message
...
If you were to start out with all cells on the sheet unlocked then you
could use a worksheet change event similar to this. This checks entry in
columm A and then locks columns C,D and E on that row:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 And Target.Column = 1 _
And Target.Value = "x" Then
Me.Unprotect Password:="mypassword"
Range(Cells(Target.Row, 3), Cells(Target.Row, 5)).Locked = True
Me.Protect Password:="mypassword"
End If
End Sub

Hope this helps
Rowan

Gary Keramidas wrote:
what would be the best way to do this:

if an x is entered in a cell, lock a range of cells in that same row so
nothing in that range could be entered?



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default lock a range question

I would be a surprised if that did give the desired result. The format
for refering to a range using the cells method is:
Range(Cells(row,column),Cells(row,column))
so you are looping through columns 4 to 56 and columns 11 to 16.
You might want just one statement:
range(cells(target.row,11),cells(target.row,16)).l ocked = true. See my
earlier reply. Aslo I am not sure where columns 11 to 16 is coming from
- I thought we were aiming for columns V to Z.

Regards
Rowan

Gary Keramidas wrote:
this appears to work, can anyone verify?

Private Sub Worksheet_Change(ByVal Target As Range)
For z = 4 To 56
For y = 11 To 16
If Target.Row = z And Target.Column = 27 _
And Target.Value = "x" Then
Range(Cells(Target.Row, z), Cells(Target.Row, y)).Locked =
True
End If
Next
Next
End Sub


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default lock a range question

that was a mis-typed line by me

--


Gary


"Rowan Drummond" wrote in message
...
I would be a surprised if that did give the desired result. The format for
refering to a range using the cells method is:
Range(Cells(row,column),Cells(row,column))
so you are looping through columns 4 to 56 and columns 11 to 16.
You might want just one statement:
range(cells(target.row,11),cells(target.row,16)).l ocked = true. See my
earlier reply. Aslo I am not sure where columns 11 to 16 is coming from -
I thought we were aiming for columns V to Z.

Regards
Rowan

Gary Keramidas wrote:
this appears to work, can anyone verify?

Private Sub Worksheet_Change(ByVal Target As Range)
For z = 4 To 56
For y = 11 To 16
If Target.Row = z And Target.Column = 27 _
And Target.Value = "x" Then
Range(Cells(Target.Row, z), Cells(Target.Row,
y)).Locked = True
End If
Next
Next
End Sub




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default lock a range question

rowan:
ran into a problem:

if i put the code on each sheet it works fine. if i remove it from each
sheet and put in in the workbook module so it can monitor multiple sheets,
it doesn't work.
when i select column aa or af and the sheet is protected, get a debug
error. if i unprotect and enter an x, it doesn't lock the range.

why would it act differently, the only difference in code is the first line?
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
instead of
Private Sub Worksheet_Change(ByVal Target As Range)
--


Gary


"Rowan Drummond" wrote in message
...
I would be a surprised if that did give the desired result. The format for
refering to a range using the cells method is:
Range(Cells(row,column),Cells(row,column))
so you are looping through columns 4 to 56 and columns 11 to 16.
You might want just one statement:
range(cells(target.row,11),cells(target.row,16)).l ocked = true. See my
earlier reply. Aslo I am not sure where columns 11 to 16 is coming from -
I thought we were aiming for columns V to Z.

Regards
Rowan

Gary Keramidas wrote:
this appears to work, can anyone verify?

Private Sub Worksheet_Change(ByVal Target As Range)
For z = 4 To 56
For y = 11 To 16
If Target.Row = z And Target.Column = 27 _
And Target.Value = "x" Then
Range(Cells(Target.Row, z), Cells(Target.Row,
y)).Locked = True
End If
Next
Next
End Sub


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default lock a range question

Hi Gary

The workbook sheetselection change event is very different from the
worksheet change event. The Workbook_SheetSelectionChange fires every
time the user clicks on a different sheet and will not track changes to
individual sheets.

You should have Worksheet_Change event behind each sheet you want to
monitor.

Regards
Rowan

Gary Keramidas wrote:
rowan:
ran into a problem:

if i put the code on each sheet it works fine. if i remove it from each
sheet and put in in the workbook module so it can monitor multiple sheets,
it doesn't work.
when i select column aa or af and the sheet is protected, get a debug
error. if i unprotect and enter an x, it doesn't lock the range.

why would it act differently, the only difference in code is the first line?
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
instead of
Private Sub Worksheet_Change(ByVal Target As Range)

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
lock range based on values loop fishy Excel Discussion (Misc queries) 3 September 28th 09 02:31 PM
Lock or Unlock Range of Cells on Worksheet_Change Event Grahame Coyle Excel Worksheet Functions 3 July 14th 08 12:27 AM
How do i can lock more than one range in worksheet Nazr-ul-Haque Excel Discussion (Misc queries) 2 August 26th 07 03:06 PM
Excel should lock individual cells or range only centrality Excel Worksheet Functions 1 June 30th 06 08:06 PM
Unable to Lock a Range Stuart[_5_] Excel Programming 7 July 10th 04 06:06 PM


All times are GMT +1. The time now is 06:30 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"