Locking and unlocking cells based on value in another cell

JCP2

New Member
Joined
Jan 29, 2009
Messages
5
Was wondering if I could get some help on VBA. I've modified some code from the following link:

http://www.mrexcel.com/forum/excel-...unlocking-cells-based-value-another-cell.html

I'd like to unlock/lock column E based on text in column H.

The spreadsheet is protected to start and column E is set to locked. Password is set to "" So if H5 = "","Scheduled","To Do", or "Ongoing" then I'd like for cell E5 to be set to locked and if H5 = "Completed" I'd like for H5 to be set to unlocked. I'd like this to be active so that when a user selects "Completed" in column H the corresponding cell in column E is unlocked and vise versa if selecting something that needs to lock the cell.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("H5:H107")) Is Nothing Then
        Dim cell As Range
        Unprotect Password:=""
        For Each cell In Intersect(Target, Range("H5:H107"))
            Select Case cell.Value
                Case "", "Scheduled", "To Do", "Ongoing"
                    cell.Offset(0, -3).Locked = True
                Case Else
                    cell.Offset(0, -3).Locked = False
            End Select
        Next cell
        Protect Password:=""
    End If
    
End Sub


Thanks for the help
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Figured it out. Works well.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("H5:H107")) Is Nothing Then
        Dim cell As Range
        Unprotect Password:=""
        For Each cell In Intersect(Target, Range("H5:H107"))
            Select Case cell.Value
                Case ""
                    cell.Offset(0, -3).Locked = True
                Case "Scheduled"
                    cell.Offset(0, -3).Locked = True
                Case "To Do"
                    cell.Offset(0, -3).Locked = True
                Case "Ongoing"
                    cell.Offset(0, -3).Locked = True
                Case Else
                    cell.Offset(0, -3).Locked = False
            End Select
        Next cell
        Protect Password:=""
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,492
Messages
6,125,115
Members
449,206
Latest member
burgsrus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top