I have a worksheet with many different columns for input. The columns in question are D, H, and I. D and H are drop downs. I is any text input. I am trying to put together a VBA script so that if a selection is made in D2 then that will lock H2 and I2. If a selection is made in H2 then that will lock D2 and I2. If text is entered in I2 then that will lock D2 and H2. Lastly, this will need to be done for the entire column of D, H, and I so that each cell in those columns have the same property ie D16 and H16 will lock if I16 is filled and so on and so forth.
If this can be implemented by formulas as well I do not mind.
` Private Sub Worksheet_Change(ByVal Target As Cells)
If ActiveSheet.Cells(2, 4).Text = True Then
'This is what I don't understand. I don't know what to set the text to. I'm trying to say if there's anything in the cell Then do the following...
ActiveSheet.Cells(2, 8).Locked = True
ActiveSheet.Cells(2, 9).Locked = True
Else
ActiveSheet.Cells(2, 8).Text = True Then
ActiveSheet.Cells(2, 4).Locked = True
ActiveSheet.Cells(2, 9).Locked = True
Else
ActiveSheet.Cells(2, 9).Text = True Then
ActiveSheet.Cells(2, 4).Locked = True
ActiveSheet.Cells(2, 8).Locked = True
End If
End Sub`
Try this code, but bear in mind locking cells as no affect unless the worksheet is protected. Also note that ByVal Target as Range
is the correct syntax for the Worksheet_Change
argument (not ByVal Target as Cells
).
Private Sub Worksheet_Change(ByVal Target As Range)
If Len(Target) Then
Select Case Target.Column
Case Is = 4
Me.Unprotect "pwd"
Cells(Target.Row, 8).Locked = True
Cells(Target.Row, 9).Locked = True
Me.Protect "pwd"
Case Is = 8
Me.Unprotect "pwd"
Cells(Target.Row, 4).Locked = True
Cells(Target.Row, 9).Locked = True
Me.Protect "pwd"
Case Is = 9
Me.Unprotect "pwd"
Cells(Target.Row, 4).Locked = True
Cells(Target.Row, 8).Locked = True
Me.Protect "pwd"
End Select
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.