Lock some cells in row based on value in column A

libcat

New Member
Joined
Nov 30, 2021
Messages
10
Platform
  1. Windows
Hi everyone,

Can anyone help me come up with a VBA code? I've tried a million ideas that I found on mrexcel but can't seem to get them to work.

I need to unlock specific cells in a row based on a specific value in column A of the same row:

If A2 = In Process, unlock B2:C2, E2, and G2:AN2
And repeat for each row: If A3 = In Process, unlock B3:C3, E3, and G3:AN3.

If the value in column A value is any other value that "In Process", all cells in that row should stay locked.

Thanks in advance!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi
Welcome to the forum
Try
VBA Code:
Sub test()
    Dim i As Long
    Dim arng As Range
    Dim rng As Range
    ActiveSheet.Unprotect ""
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i, 1) = "In Process" Then
            Set rng = Union(Range(Cells(i, 2), Cells(i, 3)), Cells(i, 5), Range(Cells(i, 7), Cells(i, 35)))
            If arng Is Nothing Then Set arng = rng Else Set arng = Union(arng, rng)
        End If
    Next
    arng.Locked = False
    ActiveSheet.Protect ""
End Sub
 
Last edited:
Upvote 0
Thanks mohadin! I've tried this code and it works, but only if I go back to VBA and hit run after setting the value in column A to In Process. Do you know how I can make it automatically run after adjusting the value in A?
 
Upvote 0
Ok
Copy this code in sheet code
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 Dim i As Long
    Dim arng As Range
    Dim rng As Range
    ActiveSheet.Unprotect ""
    For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(i, 1) = "In Process" Then
            Set rng = Union(Range(Cells(i, 2), Cells(i, 3)), Cells(i, 5), Range(Cells(i, 7), Cells(i, 35)))
            If arng Is Nothing Then Set arng = rng Else Set arng = Union(arng, rng)
         End If
    Next
    arng.Locked = False
    ActiveSheet.Protect ""
End Sub
 
Upvote 0
I'm getting this error message:
Run-time error '91': Object variable or With block variable not set

Any idea what's causing that?
 
Upvote 0
To be more specific- I get this error message when I set column A to anything other than "In Process".
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Long
    Dim rng As Range
    ActiveSheet.Unprotect ""
    If Not Intersect(Target, Range("a:a")) Is Nothing Then
        i = Target.Row
        Set rng = Union(Range(Cells(i, 2), Cells(i, 3)), Cells(i, 5), Range(Cells(i, 7), Cells(i, 35)))
        If Cells(i, 1) = "In Process" Then
            rng.Locked = False
        Else
            rng.Locked = True
        End If
        ActiveSheet.Protect ""
    End If
End Sub
 
Upvote 0
Solution
Hi mohadin... I was a bit premature with my last message. It seems like if ANY of the cells in column A contain "In Process", then cell in ALL rows are unlocked. I need it to only unlock the cells from the row with column A "In Process".
Thanks in advance!!
 
Upvote 0
I think I've fixed it. I moved the
ActiveSheet.Protect ""
to after the last End If:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Long
    Dim rng As Range
    ActiveSheet.Unprotect ""
    If Not Intersect(Target, Range("a:a")) Is Nothing Then
        i = Target.Row
        Set rng = Union(Range(Cells(i, 2), Cells(i, 3)), Cells(i, 5), Range(Cells(i, 7), Cells(i, 35)))
        If Cells(i, 1) = "In Process" Then
            rng.Locked = False
        Else
            rng.Locked = True
        End If
    End If
    ActiveSheet.Protect ""
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,917
Members
449,055
Latest member
KB13

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