Link to home
Start Free TrialLog in
Avatar of Pdeters
Pdeters

asked on

Excel worksheet update two different cells

I had this question after viewing Excel 2010 disable a cell based on another cell.

I am using this to so that if one cell is populated then another is not updatable. Is it possible to have another change option in a worksheet?

Option Explicit

Const PW As String = "123"
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
'The code will be triggered if the cell being changed is in column D, starting from Row10
If Target.Column = 4 And Target.Row > 9 Then

    ActiveSheet.Unprotect Password:=PW
    If Target <> "" Then
        Cells(Target.Row, "E").Locked = True
    Else
        Cells(Target.Row, "E").Locked = False
    End If
    ActiveSheet.Protect Password:=PW
End If
End Sub
Avatar of Pdeters
Pdeters

ASKER

Can you have more than one Target

Private Sub Worksheet_Change(ByVal Target As Range)
Avatar of Pdeters

ASKER

I guess the question is can I have more than one private Sub
Avatar of Shums Faruk
Which other columns should be disabled?
You can have same Private Sub for another range.
Avatar of Pdeters

ASKER

What I want to do is another column has a value then disable another column. columns are not related
BTW, I don't see your previous accepted solution works, I can enter any where in Col E even Col D has value or blank.
LockCells.xlsm
If you are asking if there can be more code in the change event then yes, definitely.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
'The code will be triggered if the cell being changed is in column D, starting from Row10
If Target.Column = 4 And Target.Row > 9 Then

    ActiveSheet.Unprotect Password:=PW
    If Target <> "" Then
        Cells(Target.Row, "E").Locked = True
    Else
        Cells(Target.Row, "E").Locked = False
    End If
    ActiveSheet.Protect Password:=PW
End If

If Target.Row = 27 Then
    MsgBox "This is a test"
End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pdeters

ASKER

I cannot get that to work
Can you explain in more detail exactly what you want to do?
Avatar of Pdeters

ASKER

I got it to work - Thank you so much for taking the time. It makes sense now
You're welcome Pdeters!