Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Do it for "n" cells
Hi all
My code reads one cell and through the options it locks or not others cells. Im trying to extend my programming to all subsequent cells. How can I do it? Like using "for" instruction?!?! Does anybody help me? Private Sub Worksheet_Change(ByVal Target As Range) If [E9]= "A" Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = True ActiveSheet.Range("F9").Locked = False [G9:K9].Interior.ColorIndex = 15 [F9].Interior.ColorIndex = 0 ActiveSheet.Protect ("") ElseIf Target.Cells.Text = "B" Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = False ActiveSheet.Range("F9").Locked = True [G9:K9].Interior.ColorIndex = 0 [F9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("F9:K9").Locked = True [F9:K9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") End If End Sub Thanks in advance Kelson |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Do it for "n" cells
Worksheet change functions should look something like this. This is only an
example. code may not do exactly what you need it to do. Private Sub Worksheet_Change(ByVal Target As Range) for each cell in Target If cell.column = 1 Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = True ActiveSheet.Range("F9").Locked = False [G9:K9].Interior.ColorIndex = 15 [F9].Interior.ColorIndex = 0 ActiveSheet.Protect ("") Else If cell.column = 2 Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = False ActiveSheet.Range("F9").Locked = True [G9:K9].Interior.ColorIndex = 0 [F9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("F9:K9").Locked = True [F9:K9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") end if End If next cell End Sub "Kelson" wrote: Hi all My code reads one cell and through the options it locks or not others cells. Im trying to extend my programming to all subsequent cells. How can I do it? Like using "for" instruction?!?! Does anybody help me? Private Sub Worksheet_Change(ByVal Target As Range) If [E9]= "A" Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = True ActiveSheet.Range("F9").Locked = False [G9:K9].Interior.ColorIndex = 15 [F9].Interior.ColorIndex = 0 ActiveSheet.Protect ("") ElseIf Target.Cells.Text = "B" Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = False ActiveSheet.Range("F9").Locked = True [G9:K9].Interior.ColorIndex = 0 [F9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("F9:K9").Locked = True [F9:K9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") End If End Sub Thanks in advance Kelson |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Do it for "n" cells
Well
Is not exactly like that. I mean, i would like to lock/unlock cells based on value selected in another cell. Selected value in Ex Range Gx:Kx E9,E10,E11,E12 and all subsequent cells Any help? Cheers "Joel" escreveu: Worksheet change functions should look something like this. This is only an example. code may not do exactly what you need it to do. Private Sub Worksheet_Change(ByVal Target As Range) for each cell in Target If cell.column = 1 Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = True ActiveSheet.Range("F9").Locked = False [G9:K9].Interior.ColorIndex = 15 [F9].Interior.ColorIndex = 0 ActiveSheet.Protect ("") Else If cell.column = 2 Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = False ActiveSheet.Range("F9").Locked = True [G9:K9].Interior.ColorIndex = 0 [F9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("F9:K9").Locked = True [F9:K9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") end if End If next cell End Sub |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Do it for "n" cells
Let me explain how worksheet change works. the target parameter can be one
or many cells. If you change one cell then target is only one cell. If you do a copy and paste of more than one cell on the worksheet then Target is many cells. the statement "for each cell in Target" allows you do check all cells when a copy and paste is performed. From cell (without the s at the end) you can get the row number or column number (cell.row and cell.column). if you want to check another cell in the same row as cell you would use if cells(.cell.row,"D") = 'this value" Notice there is a cell with and without the 's'. I don't completly understnad what you want but I made some changes to the code as an example Private Sub Worksheet_Change(ByVal Target As Range) for each cell in Target If cell.column = 1 Then ActiveSheet.Unprotect ("") if cells(cell.row,"G").locked = true then <= NEW ActiveSheet.Range("G9:K9").Locked = True ActiveSheet.Range("F9").Locked = False [G9:K9].Interior.ColorIndex = 15 [F9].Interior.ColorIndex = 0 ActiveSheet.Protect ("") end if Else If cell.column = 2 Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = False ActiveSheet.Range("F9").Locked = True [G9:K9].Interior.ColorIndex = 0 [F9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("F9:K9").Locked = True [F9:K9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") end if End If next cell End Sub "Kelson" wrote: Well Is not exactly like that. I mean, i would like to lock/unlock cells based on value selected in another cell. Selected value in Ex Range Gx:Kx E9,E10,E11,E12 and all subsequent cells Any help? Cheers "Joel" escreveu: Worksheet change functions should look something like this. This is only an example. code may not do exactly what you need it to do. Private Sub Worksheet_Change(ByVal Target As Range) for each cell in Target If cell.column = 1 Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = True ActiveSheet.Range("F9").Locked = False [G9:K9].Interior.ColorIndex = 15 [F9].Interior.ColorIndex = 0 ActiveSheet.Protect ("") Else If cell.column = 2 Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = False ActiveSheet.Range("F9").Locked = True [G9:K9].Interior.ColorIndex = 0 [F9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("F9:K9").Locked = True [F9:K9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") end if End If next cell End Sub |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Do it for "n" cells
Check your other thread.
Kelson wrote: Hi all My code reads one cell and through the options it locks or not others cells. Im trying to extend my programming to all subsequent cells. How can I do it? Like using "for" instruction?!?! Does anybody help me? Private Sub Worksheet_Change(ByVal Target As Range) If [E9]= "A" Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = True ActiveSheet.Range("F9").Locked = False [G9:K9].Interior.ColorIndex = 15 [F9].Interior.ColorIndex = 0 ActiveSheet.Protect ("") ElseIf Target.Cells.Text = "B" Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G9:K9").Locked = False ActiveSheet.Range("F9").Locked = True [G9:K9].Interior.ColorIndex = 0 [F9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("F9:K9").Locked = True [F9:K9].Interior.ColorIndex = 15 ActiveSheet.Protect ("") End If End Sub Thanks in advance Kelson -- Dave Peterson |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Do it for "n" cells
Hi Joel
I got it working! Have a look on it: Private Sub Worksheet_Change(ByVal Target As Range) n = Target.Cells.Row If Range("E" & n).Value = "A Then ActiveSheet.Unprotect ("") ActiveSheet.Range("G" & n & ":K" & n).Locked = True ActiveSheet.Range("F" & n).Locked = False ActiveSheet.Protect ("") Else ActiveSheet.Unprotect ("") ActiveSheet.Range("G" & n & ":K" & n).Locked = False ActiveSheet.Range("F" & n).Locked = True ActiveSheet.Protect ("") End If End Sub Thank you anyway! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell | Excel Discussion (Misc queries) | |||
Check if cells contain the word "Thailand", return "TRUE" | Excel Worksheet Functions | |||
Do mighty exchange "pixels" on "mm" - cells (width, height)? :( | Excel Programming | |||
Can you "duplicate" "copy" listboxes and code to multiple cells? | Excel Programming | |||
Using "Cells" to write "Range("A:A,H:H").Select" | Excel Programming |