If cells contain certain text, how to lock adjacent cells with Format cells -> Protection -> Locked

K0st4din

Active Member
Joined
Feb 8, 2012
Messages
488
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello, everyone,
I searched the internet for a solution to my case, but I can't get it to work.
I am looking for an option with which I suppose with a macro (maybe without a macro) a check is made to see if the numbers 1, 2 or 3 are contained in cells A36, 37, 38. If there are, then rows B36, 37, 38 should be locked.
The worksheet is locked, I am looking if it is possible to use Format cells -> Protection -> Locked .
Thank you in advance for your assistance!
АВАНСОВ ОТЧЕТ ИМЕ ФАМИЛИЯ МЕСЕЦ ГОДИНА(DOBAVQNE)-edited 01112023.xls
AB
4202311
5
6dayform
7mounthslegend
81
92
103
114
125
136
147
158
169
1710
1811
1912
2013
2114
2215
2316
2417
2518
2619
2720
2821
2922
3023
3124
3225
3326
3427
3528
3629
3730
381
ИМЕ СЛУЖИТЕЛ
Cell Formulas
RangeFormula
A8A8=DATE(godina,$B$4,1)
A9A9=DATE(godina,$B$4,2)
A10A10=DATE(godina,$B$4,3)
A11A11=DATE(godina,$B$4,4)
A12A12=DATE(godina,$B$4,5)
A13A13=DATE(godina,$B$4,6)
A14A14=DATE(godina,$B$4,7)
A15A15=DATE(godina,$B$4,8)
A16A16=DATE(godina,$B$4,9)
A17A17=DATE(godina,$B$4,10)
A18A18=DATE(godina,$B$4,11)
A19A19=DATE(godina,$B$4,12)
A20A20=DATE(godina,$B$4,13)
A21A21=DATE(godina,$B$4,14)
A22A22=DATE(godina,$B$4,15)
A23A23=DATE(godina,$B$4,16)
A24A24=DATE(godina,$B$4,17)
A25A25=DATE(godina,$B$4,18)
A26A26=DATE(godina,$B$4,19)
A27A27=DATE(godina,$B$4,20)
A28A28=DATE(godina,$B$4,21)
A29A29=DATE(godina,$B$4,22)
A30A30=DATE(godina,$B$4,23)
A31A31=DATE(godina,$B$4,24)
A32A32=DATE(godina,$B$4,25)
A33A33=DATE(godina,$B$4,26)
A34A34=DATE(godina,$B$4,27)
A35A35=DATE(godina,$B$4,28)
A36A36=DATE(godina,$B$4,29)
A37A37=DATE(godina,$B$4,30)
A38A38=DATE(godina,$B$4,31)
Named Ranges
NameRefers ToCells
godina='ИМЕ СЛУЖИТЕЛ'!$A$4A8:A38
Cells with Data Validation
CellAllowCriteria
B8:B38List=$B$40:$B$46
A4List=$T$2:$T$32
B4List=$U$2:$U$13
 
It works perfectly fine for me.
Have you accidentally disabled events from running in your code?
Try MANUALLY running the following code to re-enable them:
VBA Code:
Sub ReEnableEvents()
    Application.EnableEvents = True
End Sub

And add the lines in red to your current code for debugging:
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
   
    Dim rng As Range
    Dim cell As Range
   
    MsgBox "VBA code is running!"
   
'   Check to see if update made to cell B4
    Set rng = Intersect(Target, Range("B4"))
   
'   Exit code if no changes made to watched range
    If rng Is Nothing Then Exit Sub
   
    MsgBox "Cell B4 was manually updated!"
   
'   Unprotext sheet
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:="123"

'   Loop through range A36:A38
    For Each cell In Range("A36:A38")
'       See if value of cell is greater than or equal to 3
        If cell.Value >= 3 Then
            cell.Offset(0, 1).Locked = False
'       See if value of cell is less than 3
        ElseIf cell.Value < 3 Then
            cell.Offset(0, 1).Locked = True
        End If
    Next cell
   
'   Reprotect sheet
    ActiveSheet.Protect Password:="123"
    Application.ScreenUpdating = True
   
End Sub
Now try updating cell B4. You should get the two Message Boxes. If you do not, then you have done something wrong, most likely:
1. Have placed the VBA code in the wrong module
2. Have not enabled VBA code to run
3. Are not updating cell B4 on your sheet manually
4. Are working across multiple sheets instead of just within one sheet
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
2023-12-05_163032.jpg


Yes, with what you added, everything says that the macro is working. That manually changed B4, etc., but when I unlock the worksheet, the picture shows that there is no beak, the cells are locked, and all three cells meet the condition.

PS - I just didn't understand, the last sentence, that I work in several books, and I am only in this workbook and in this worksheet. Except I have 5 more but I'm not interested in them right now. This Bananas worksheet of mine is called "EMPLOYEE NAME" but I'm changing it.


If I may say again, if there are 1, 2 or 3 in A, cells B - they cannot write. This is the shortest explanation
 
Upvote 0
In going back over the sheet, I think I see your issue - you do NOT have numbers like 1,2,3 in cells A36:A38.
Rather, you have date formulas that are formatted just to show the day portion. So you cannot use a simple check of the displayed value.
Formulas run off the actual underlying values, not the displayed values.

So we need to pull the "Day" value off to do our check, i.e.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rng As Range
    Dim cell As Range
    
'   Check to see if update made to cell B4
    Set rng = Intersect(Target, Range("B4"))
    
'   Exit code if no changes made to watched range
    If rng Is Nothing Then Exit Sub
    
'   Unprotext sheet
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:="123"
    
'   Loop through range A36:A38
    For Each cell In Range("A36:A38")
'       See if value of cell is greater than or equal to 3
        If Day(cell.Value) >= 3 Then
            cell.Offset(0, 1).Locked = False
'       See if value of cell is less than 3
        ElseIf Day(cell.Value) < 3 Then
            cell.Offset(0, 1).Locked = True
        End If
    Next cell
    
'   Reprotect sheet
    ActiveSheet.Protect Password:="123"
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 1
Solution
Yes, yes, yes, now it works perfectly. This is exactly what should have happened. At the very beginning, when I uploaded the table, I thought that it would be seen that I had formulas there and that I was taking a day. Apparently that's where the confusion about these numbers came from. It's perfect now. If I could, I would give countless likes and thanks for this help! I wish you good health and many happy moments. Thank you from the bottom of my heart!
 
Upvote 0
You are welcome! I am glad I could help!
 
Upvote 1

Forum statistics

Threads
1,215,147
Messages
6,123,297
Members
449,095
Latest member
Chestertim

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