Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Lock and protect ranges

I've adapted some code I found on this group to protect rows that have
text in column A. Here is the code which runs on the Save event:
Dim i As Integer
ActiveSheet.Unprotect
For i = 1 To Range("A65536").End(xlUp).Row
If Cells(i, 1).Value < "" Then
Cells(i, 1).EntireRow.Locked = True
Else
Cells(i, 1).EntireRow.Locked = False
End If
Next i
ActiveSheet.Protect

Interestingly I discovered that when I only had one row of information
not only was that row protected but also all the rows below it.
However when I inserted an additional row both rows with text in
column A were protected but other rows could be edited as originally
intended.

Can anyone explain why this is happening. It took me some while to
fathom.

Many thanks in anticipation

Simon
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Lock and protect ranges


Can anyone explain why this is happening. It took me some while to
fathom.



Yes. Yor code works upwards from the last populated row in column A so none
of the rows below that are changed by your code so when you protect the sheet
all cells below your last row of data are locked (The default) and become
protected when you protect the sheet.

Rows above your last row are unlocked if column A is empty and so don't
become protected.

What do you want it to do?

Mike

"simon" wrote:

I've adapted some code I found on this group to protect rows that have
text in column A. Here is the code which runs on the Save event:
Dim i As Integer
ActiveSheet.Unprotect
For i = 1 To Range("A65536").End(xlUp).Row
If Cells(i, 1).Value < "" Then
Cells(i, 1).EntireRow.Locked = True
Else
Cells(i, 1).EntireRow.Locked = False
End If
Next i
ActiveSheet.Protect

Interestingly I discovered that when I only had one row of information
not only was that row protected but also all the rows below it.
However when I inserted an additional row both rows with text in
column A were protected but other rows could be edited as originally
intended.

Can anyone explain why this is happening. It took me some while to
fathom.

Many thanks in anticipation

Simon

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Lock and protect ranges


As an afterthought if you want to be able to edit empty rows after execution
of the code it's a single line addition to what you have already

Dim i As Integer
ActiveSheet.Unprotect
ActiveSheet.Cells.Locked = False 'additional line
For i = 1 To Range("A65536").End(xlUp).Row
If Cells(i, 1).Value < "" Then
Cells(i, 1).EntireRow.Locked = True
Else
Cells(i, 1).EntireRow.Locked = False
End If
Next i
ActiveSheet.Protect
End Sub

Mike

"Mike H" wrote:

Can anyone explain why this is happening. It took me some while to
fathom.



Yes. Yor code works upwards from the last populated row in column A so none
of the rows below that are changed by your code so when you protect the sheet
all cells below your last row of data are locked (The default) and become
protected when you protect the sheet.

Rows above your last row are unlocked if column A is empty and so don't
become protected.

What do you want it to do?

Mike

"simon" wrote:

I've adapted some code I found on this group to protect rows that have
text in column A. Here is the code which runs on the Save event:
Dim i As Integer
ActiveSheet.Unprotect
For i = 1 To Range("A65536").End(xlUp).Row
If Cells(i, 1).Value < "" Then
Cells(i, 1).EntireRow.Locked = True
Else
Cells(i, 1).EntireRow.Locked = False
End If
Next i
ActiveSheet.Protect

Interestingly I discovered that when I only had one row of information
not only was that row protected but also all the rows below it.
However when I inserted an additional row both rows with text in
column A were protected but other rows could be edited as originally
intended.

Can anyone explain why this is happening. It took me some while to
fathom.

Many thanks in anticipation

Simon

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Lock and protect ranges


In fact you can now dispense with the ELSE statement becuase it will never
execute

Dim i As Integer
ActiveSheet.Unprotect
ActiveSheet.Cells.Locked = False
For i = 1 To Range("A65536").End(xlUp).Row
If Cells(i, 1).Value < "" Then
Cells(i, 1).EntireRow.Locked = True
End If
Next i
ActiveSheet.Protect

Mike

"Mike H" wrote:

As an afterthought if you want to be able to edit empty rows after execution
of the code it's a single line addition to what you have already

Dim i As Integer
ActiveSheet.Unprotect
ActiveSheet.Cells.Locked = False 'additional line
For i = 1 To Range("A65536").End(xlUp).Row
If Cells(i, 1).Value < "" Then
Cells(i, 1).EntireRow.Locked = True
Else
Cells(i, 1).EntireRow.Locked = False
End If
Next i
ActiveSheet.Protect
End Sub

Mike

"Mike H" wrote:

Can anyone explain why this is happening. It took me some while to
fathom.



Yes. Yor code works upwards from the last populated row in column A so none
of the rows below that are changed by your code so when you protect the sheet
all cells below your last row of data are locked (The default) and become
protected when you protect the sheet.

Rows above your last row are unlocked if column A is empty and so don't
become protected.

What do you want it to do?

Mike

"simon" wrote:

I've adapted some code I found on this group to protect rows that have
text in column A. Here is the code which runs on the Save event:
Dim i As Integer
ActiveSheet.Unprotect
For i = 1 To Range("A65536").End(xlUp).Row
If Cells(i, 1).Value < "" Then
Cells(i, 1).EntireRow.Locked = True
Else
Cells(i, 1).EntireRow.Locked = False
End If
Next i
ActiveSheet.Protect

Interestingly I discovered that when I only had one row of information
not only was that row protected but also all the rows below it.
However when I inserted an additional row both rows with text in
column A were protected but other rows could be edited as originally
intended.

Can anyone explain why this is happening. It took me some while to
fathom.

Many thanks in anticipation

Simon

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
Protect and lock cell in Pivottable Hagge Excel Worksheet Functions 1 April 18th 08 09:57 PM
how to only protect/lock cell formats? Glen Excel Discussion (Misc queries) 0 October 10th 06 01:19 AM
Protect or Lock Column Tom LeBold Excel Programming 3 March 25th 05 08:53 AM
Lock and protect cells without protect the sheet Christian[_7_] Excel Programming 6 December 28th 04 04:50 PM
Protect and Lock Problem SOLVED!! Anon y mous Excel Programming 0 June 29th 04 06:08 AM


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

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

About Us

"It's about Microsoft Excel"