Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
CM CM is offline
external usenet poster
 
Posts: 136
Default macro to set cell protection by row, based on pre-populated values

I got the code below from one of Dave Peterson's posts -- to set cell
protection based on a value input. Works fine.

For the initial pass, however, the values in the spreadsheet (col b) are
already populated and I need to modify the code for a one-time use to set the
protection of column A using the existing values in column B instead of the
macro running when the worksheet is changed. I don't know how to do this. Any
help appreciated.

--
cm
  #2   Report Post  
Posted to microsoft.public.excel.programming
CM CM is offline
external usenet poster
 
Posts: 136
Default macro to set cell protection by row, based on pre-populated values

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

'one cell at a time
If Target.Cells.Count 1 Then Exit Sub

'only in column B
If Intersect(Target, Me.Range("B:B")) Is Nothing Then Exit Sub

On Error GoTo ErrHandler:

Me.Unprotect
Target.Offset(0, -1).Locked = CBool(Target.Value = 0)
' target.offset(row,column)
' target.value = 0 means false, 1 means true


ErrHandler:
Me.Protect
Application.EnableEvents = True
End Sub

--
hope to help,
cm


"cm" wrote:

I got the code below from one of Dave Peterson's posts -- to set cell
protection based on a value input. Works fine.

For the initial pass, however, the values in the spreadsheet (col b) are
already populated and I need to modify the code for a one-time use to set the
protection of column A using the existing values in column B instead of the
macro running when the worksheet is changed. I don't know how to do this. Any
help appreciated.

--
cm

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default macro to set cell protection by row, based on pre-populated values

Maybe something like:

Option Explicit
Sub RunOneTime()

Dim myCell As Range
Dim myRng As Range
Dim wks As Worksheet

Set wks = ActiveSheet

With wks
.Unprotect Password:="no password???"

Set myRng = .Range("b1", .Cells(.Rows.Count, "B").End(xlUp))

For Each myCell In myRng.Cells
myCell.Offset(0, -1).Locked = CBool(myCell.Value = 0)
Next myCell

.Protect Password:="no password???"
End With

End Sub

This code would go in a General module--not behind a worksheet.

cm wrote:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

'one cell at a time
If Target.Cells.Count 1 Then Exit Sub

'only in column B
If Intersect(Target, Me.Range("B:B")) Is Nothing Then Exit Sub

On Error GoTo ErrHandler:

Me.Unprotect
Target.Offset(0, -1).Locked = CBool(Target.Value = 0)
' target.offset(row,column)
' target.value = 0 means false, 1 means true


ErrHandler:
Me.Protect
Application.EnableEvents = True
End Sub

--
hope to help,
cm

"cm" wrote:

I got the code below from one of Dave Peterson's posts -- to set cell
protection based on a value input. Works fine.

For the initial pass, however, the values in the spreadsheet (col b) are
already populated and I need to modify the code for a one-time use to set the
protection of column A using the existing values in column B instead of the
macro running when the worksheet is changed. I don't know how to do this. Any
help appreciated.

--
cm


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default macro to set cell protection by row, based on pre-populated values

Option Explicit

Sub test()
Dim rng As Range
Dim ocell As Range
Set rng = Range(Cells(1, 2), Cells(Rows.Count, 2).End(xlUp))
ActiveSheet.Unprotect
For Each ocell In rng
If ocell.Value < "" Then
ocell.Offset(0, -1).Locked = True
End If
Next ocell
ActiveSheet.Protect
End Sub


Gord Dibben MS Excel MVP


On Wed, 11 Nov 2009 14:22:07 -0800, cm wrote:

I got the code below from one of Dave Peterson's posts -- to set cell
protection based on a value input. Works fine.

For the initial pass, however, the values in the spreadsheet (col b) are
already populated and I need to modify the code for a one-time use to set the
protection of column A using the existing values in column B instead of the
macro running when the worksheet is changed. I don't know how to do this. Any
help appreciated.


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
Macro that will add multiple emails based on a range of cell values Tysone Excel Programming 2 May 27th 08 04:52 PM
Macro to Create Worksheets Based on Cell Values Lilbit Excel Worksheet Functions 3 March 24th 08 05:39 PM
Payment cell populated based on date formula TonyD Excel Discussion (Misc queries) 6 January 31st 07 09:55 AM
Compiling macro based on cell values simonsmith Excel Discussion (Misc queries) 1 May 16th 06 08:31 PM
Copy Values to Below last Populated Cell Q John Excel Programming 2 June 29th 04 05:28 PM


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