VBA Lock Cell in row if yes in selected

FN1234

New Member
Joined
Feb 2, 2018
Messages
3
Hello, I have the following code to lock the entire row, if the cell selects Yes, but how can I just lock the row from column a to Cd if cell.value is yes.

I also then want to say if cell.value="No" , unlock the row and if it's "" do nothing to the row.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

For Each cell In Range("CE7:CE357")
If cell.Value = "Yes" Then
ActiveSheet.Unprotect "Password"

cell.EntireRow.Locked = True

ActiveSheet.Protect "Password"

End If

Next cell
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the Board!

Firstly, I think you are using the wrong Event Procedure. The way you have written, any time you move in your workbook (select a different cell), it is going to evaluate all 351 rows! Every time!
And I don't think you want your protect/unprotect lines in the loop, as that will unlock/lock 351 times each time it runs too! You should only unlock/lock it once per time the procedure it called.
These things are a bit overkill and might slow your workbook down.

I believe that you really only need it to run when you change a value in CE7:CE57, and only need to check the row(s) you are changing. So a Worksheet_Change event procedure is probably more appropriate, like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim isect As Range
    Dim cell As Range
    Dim rw As Long
    
    Set isect = Intersect(Target, Range("CE7:CE357"))
    
    If Not isect Is Nothing Then
        ActiveSheet.Unprotect "Password"
        For Each cell In isect
            rw = cell.Row
            Select Case UCase(cell.Value)
                Case "YES"
                    Range(Cells(rw, "A"), Cells(rw, "CD")).Locked = True
                Case "NO"
                    Range(Cells(rw, "A"), Cells(rw, "CD")).Locked = False
            End Select
        Next cell
        ActiveSheet.Protect "Password"
    End If
    
End Sub
 
Last edited:
Upvote 0
You are welcome! Glad I was able to help!

Hope it all makes sense (let me know if you have any specific questions about the code).
 
Upvote 0
What is the best way to learn VBA?

I never learned in a traditional setting, just doing it as I go for work, but I feel like my code isn't always the most efficient.
 
Upvote 0
What is the best way to learn VBA?
Well, it depends on the person. Some are find at learning from books, others are more visual and like demonstrations. MrExcel has a lot of books in their bookstore for various different levels (https://www.mrexcel.com/store/).

Hiker95 has also put together a very extensive list of on-line sources:
https://www.mrexcel.com/forum/lounge-v-2-0/1031751-two-novice-questions-post4952119.html#post4952119

And of course, hanging around here and reading through various threads that interest you will help augment your knowledge and skills!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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