Lock Cells with certain texts

DonPaolo

New Member
Joined
Mar 11, 2018
Messages
3
Hi All!

New here! Been reading this forum for some very helpful tips and solutions but 1st time to register as I cant seem to find a solution to what I need to do.

Could you give me a marco command to lock cells that contains specific texts (e.g. cells containing "new report", or "Completed", or "End")

Thank you in advance!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Can the texts be anywhere in the sheet or are they in a specific column or row? Also, is one of the values "new report" or "New Report"?
 
Upvote 0
Start by unlocking all the cells in your sheet and protect the sheet with a password of your choosing. Change "myPassword" in the code to match your password (2 occurrences). This macro assumes that the values can be anywhere on your sheet. If they are in a particular column, please let me know and I will modify the macro although the macro below will work in both cases.
Code:
Option Compare Text
Sub CopyRange()
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:="myPassword"
    Dim myArray As Variant
    Dim foundVal As Range
    Dim i As Long
    Dim sAddr As String
    myArray = Array("new report", "Completed", "End")
    For i = LBound(myArray) To UBound(myArray)
        Set foundVal = ActiveSheet.UsedRange.Find(myArray(i), LookIn:=xlValues, lookat:=xlWhole)
        If Not foundVal Is Nothing Then
            sAddr = foundVal.Address
            Do
                foundVal.Locked = True
                Set foundVal = ActiveSheet.UsedRange.FindNext(foundVal)
            Loop While foundVal.Address <> sAddr
        End If
        sAddr = ""
    Next i
    ActiveSheet.Protect Password:="myPassword"
    ActiveSheet.EnableSelection = xlUnlockedCells
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Can the texts be anywhere in the sheet or are they in a specific column or row? Also, is one of the values "new report" or "New Report"?


Hi Mumps, its anywhere in the sheet. In one of the values, meant to say "New Report" :)
 
Upvote 0
OK. Try this macro:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:="myPassword"
    Dim myArray As Variant
    Dim foundVal As Range
    Dim i As Long
    Dim sAddr As String
    myArray = Array("New Report", "Completed", "End")
    For i = LBound(myArray) To UBound(myArray)
        Set foundVal = ActiveSheet.UsedRange.Find(myArray(i), LookIn:=xlValues, lookat:=xlWhole)
        If Not foundVal Is Nothing Then
            sAddr = foundVal.Address
            Do
                foundVal.Locked = True
                Set foundVal = ActiveSheet.UsedRange.FindNext(foundVal)
            Loop While foundVal.Address <> sAddr
        End If
        sAddr = ""
    Next i
    ActiveSheet.Protect Password:="myPassword"
    ActiveSheet.EnableSelection = xlUnlockedCells
    Application.ScreenUpdating = True
End Sub
Please refer to Post #3 for some instructions.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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