ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   lock a cell based on cell value (https://www.excelbanter.com/excel-discussion-misc-queries/214302-lock-cell-based-cell-value.html)

Texins Karate

lock a cell based on cell value
 
I am running Excel 2003 with a spreadsheet that has Activity in column A,
Status (Open or Closed) in column B, Days of Week (Sun thru Sat) in columns C
thru I. I am looking for a way to "lock" columns C thru I if column B is
"Closed" for each row of Activity in Column A

example:

Col A Col B Col C Col D Col E Col F
Activity 1 Closed lock lock lock lock
Activity 2 Open unlock unlock unlock unlock

Gary''s Student

lock a cell based on cell value
 
Assume that we begin with the worksheet unprotected and all cells unlocked.
Install this event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim t As Range
Dim b As Range
Dim trow As Long
Set t = Target
Set b = Range("B:B")
If Intersect(t, b) Is Nothing Then Exit Sub
If t.Value < "Closed" Then Exit Sub
trow = t.Row
ActiveSheet.Unprotect
Range("C" & trow & ":I" & trow).Locked = True
ActiveSheet.Protect
End Sub


Because it is worksheet code, it is very easy to install and automatic to use:

1. right-click the tab name near the bottom of the Excel window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it.


To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

--
Gary''s Student - gsnu200821


"Texins Karate" wrote:

I am running Excel 2003 with a spreadsheet that has Activity in column A,
Status (Open or Closed) in column B, Days of Week (Sun thru Sat) in columns C
thru I. I am looking for a way to "lock" columns C thru I if column B is
"Closed" for each row of Activity in Column A

example:

Col A Col B Col C Col D Col E Col F
Activity 1 Closed lock lock lock lock
Activity 2 Open unlock unlock unlock unlock



All times are GMT +1. The time now is 12:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com