Lock certain cells in the same row based on updates by a formula

intensified89

New Member
Joined
Mar 16, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to lock certain cells within the same row whenever one of the cell is being updated by a formula. This will be repeated for multiple rows. Currently I have the following macro, but it only allows the update whenever the user manually inputs, but doesn't work for updates by formula.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    ActiveSheet.Unprotect
   
    If Not Intersect(Target, Range("AM:AM")) Is Nothing Then Exit Sub

    Select Case Target.Value
        Case "L"
            Range("T" & Target.Row & ":AA" & Target.Row).Locked = True
            Range("AG" & Target.Row & ":AH" & Target.Row).Locked = True
        Case Else
            Range("T" & Target.Row & ":AA" & Target.Row).Locked = False
            Range("AG" & Target.Row & ":AH" & Target.Row).Locked = False
    End Select
    
    ActiveSheet.Protect UserInterfaceOnly:=True, AllowFiltering:=True
    
End Sub
 
Thanks. Strangely I get the Run-time error "13": Type mismatch for the following:

VBA Code:
       If cel.Value = "L" Then
 
Upvote 0

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.
No reason a mismatch should occur that I can see but If you havent resolved this then try changing

VBA Code:
DIM cel as range
to
VBA Code:
DIM cel as variant

OR do the follwoing
before the IF cel.vale line add this

VBA Code:
msgbox cel.value & " " & cel.address

and then run the code again.
Alternatively if you are able to post a sample sheet it would help
 
Last edited:
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Lock certain cells in the same row based on updates by a formula - OzGrid Free Excel/VBA Help Forum
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,216,079
Messages
6,128,687
Members
449,464
Latest member
againofsoul

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