VBA code to conditionally lock specific based on value in other cell

azzurri_77

New Member
Joined
Jul 30, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi all,


I have a range from D9 to D25. I want to lock D14 to D25 if D9:D13 = Yes. and then If D14:D20 =Yes, then lock D9:13 and D21:D25. and finally lock D21:D25 if D9:D20 = Yes.

Your help is much appreciated.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Kind of a meh solution, but try this - place in the Worksheet stub that you want to lock.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim countOfYes As Integer
   
    Worksheets("Sheet1").Unprotect
   
    For i = 9 To 13
        If Cells(i, 4) = "Yes" Then
            countOfYes = 1
            Exit For
        End If
    Next i
   
    If countOfYes = 1 Then
        Range("D14:D25").Locked = True
        countOfYes = 0
    Else
        Range("D14:D25").Locked = False
    End If
   
    For i = 14 To 20
        If Cells(i, 4) = "Yes" Then
            countOfYes = 1
            Exit For
        End If
    Next i
   
    If countOfYes = 1 Then
        Range("D9:D13").Locked = True
        countOfYes = 0
    Else
        Range("D9:D13").Locked = False
    End If
   
    For i = 9 To 20
        If Cells(i, 4) = "Yes" Then
            countOfYes = 1
            Exit For
        End If
    Next i
   
    If countOfYes = 1 Then
        Range("D21:D25").Locked = True
        countOfYes = 0
    Else
        Range("D21:D25").Locked = False
    End If
   
    Worksheets("Sheet1").Protect
End Sub
 
Upvote 0
Thanks Rich, this code would work, but maybe I didn't explain well, what I am trying to achieve. Range from D9 to D25. I want to lock D14 to D25 if any of the values in D9:D13 = Yes. and then If any of the values in D14:D20 range =Yes, then lock D9:13 and D21:D25. and finally lock D21:D25 if any value in the D9:D20 range = Yes. the solution that you provided will only work if all values in the range = Yes. again thanks for your help.
 
Upvote 0
The code locks the cells if any "Yes" is found; not all cells have to equal "Yes". Each loop breaks out if any Yes is found in your defined ranges.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,213
Members
448,554
Latest member
Gleisner2

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