Locking and formatting cell based on variable values of other cells

PRADEEPSWALSE

New Member
Joined
Nov 27, 2018
Messages
28
Office Version
  1. 2013
Platform
  1. Windows
Hi,
I need help in excel for locking and formatting cell based on variable values of other cells

If A1 cell is having value of “CL3” or “GP1” or “GP2“ or “GP3“ or “GP4“ or “SB1“ or “SB2“ or “SB3“ or “SF1“ or “SF2“ or “SF3“ or “SP1“ or “SP2“ or “TB1“ or “TP1“ or “TP2“ or “TP3” etc. then B1 cell should be locked and same to be filled with red colour and if value other than above then B1 cell to be unlocked for other data input.

Same is applicable to A2, A3, A4…… and B2, B3, B4….. approx.. 500 rows.


Regards,

Pradeep S. Walse
 
Hi,

If I run macro 'LockCells' manually then it works.

Is it possible to run macro after entering value in certain cells?
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
What are the "certain cells"?
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a value in column A and press the RETURN key.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect
    Dim LastRow As Long, arr As Variant, i As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    arr = Array("CL", "GP", "SB", "SF", "SP", "TB", "TP")
    For i = LBound(arr) To UBound(arr)
        Select Case True
            Case Target Like arr(i) & "*"
                With Target.Offset(, 1)
                    .Locked = True
                    .Interior.ColorIndex = 3
                End With
                Exit For
            Case Else
                With Target.Offset(, 1)
                    .Locked = False
                    .Interior.ColorIndex = xlNone
                End With
        End Select
    Next i
    ActiveSheet.Protect
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,

Thanks for the solution.

It works good.

Sorry for late reply and once again thanks.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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