Lock Entire Row based on cell specific text

Louis12

New Member
Joined
Jul 30, 2014
Messages
7
Hi,

I have a excel sheet named "Download", in this sheet i have column A to Z containing data and from row 2 to row 2000.

In column V i have the following text "01.01.2014 - PMT".

1. I need some code to check if each cell in column V contains the letters PMT and if it finds it it must lock the entire row from editing, if it does not find the word PMT in column V it must ignore the and move to the next row until done. This part is when the Worksheet is opened.

2. When I change a cell in column V to contain the letters PMT and then press enter it must automaticaly lock the entire row.

I have looked at all the code related to Lock Cell/Row on this forum and can't find something that fits the need.

Thank You for all your help.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
OK, first of all, if your row is locked after a change in V, then there is no reason to redo the check on opening the workbook. All the rows are locked.

So I have written two macros:
  1. The first macro is entered in the module for the sheet that contains this column V (if it needs to be done for more sheets, just copy it to each).
  2. The 2nd macro runs through the whole column V and locks each row with PMT. This is so that you can lock the rows at the start, or after a multi-cell copy. The macro needs to be in a standard module. It can then be activated through Alt-F8


  • Go to the sheet and right click on the name tab.
  • Select View Code...
  • The macro editor opens in the Sheet Module. This is where 'events' for this sheet can be monitored and acted on.
  • Paste the following macro there:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(Target, Columns("V")) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">' the changed cell is in column V</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> Target.Cells.Count = 1 <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#007F00">' only act if it is a single cell, so multicell copy will not trigger this</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> InStr(1, Target.Value, "PMT") <SPAN style="color:#00007F">Then</SPAN><br>                Target.EntireRow.Locked = <SPAN style="color:#00007F">True</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


  • In the window to the left where you have pasted the macro, you see the workbook and its sheets. Right click on the the 'ThisWorkbook' and select Insert / Module
  • This time a standard macro module opens. Here you can paste the following macro:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><br><SPAN style="color:#00007F">Sub</SPAN> LockPMT()<br><SPAN style="color:#007F00">' Lock those rows where Column V contains "PMT"</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> rChk <SPAN style="color:#00007F">As</SPAN> Range, r1st <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> r1st = Columns("V").Find(What:="PMT", _<br>            after:=Cells(Rows.Count, "V"), _<br>            LookIn:=xlValues, lookat:=xlPart, _<br>            searchdirection:=xlNext)<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> r1st <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">' PMT is found</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> rChk = r1st<br>        <SPAN style="color:#00007F">Do</SPAN><br>            rChk.EntireRow.Locked = <SPAN style="color:#00007F">True</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> rChk = Columns("V").FindNext(after:=rChk)<br>        <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> rChk.Address <> r1st.Address <SPAN style="color:#007F00">' else endless loop</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> r1st = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rChk = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>



  • OK, now go back to the spreadsheet.
  • type something in column V. If it contains PMT the row will be instantly locked.
  • Now press Alt-F8 rin the macro and now all the rows with PMT are locked. Save the workbook (in Excel 2007+ save as xlsm, macro enabled)
 
Upvote 0
Hi, Thank you for the code but I think there is something wrong as it does not lock the rows as I am able to delete/modify the data in each cell from Column A to V once i have pressed ALT-F8.

Also I am able to delete/modify existing data that contains in Column "V" ****PMT

if I protect the Worksheet or Workbook I then get a error on:
Macro 1 = Target.EntireRow.Locked = True
Macro 2 = rChk.EntireRow.Locked = True
 
Upvote 0
Ah, yes, small oversight on my behalf.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> Intersect(Target, Columns("V")) <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#007F00">' the changed cell is in column V</SPAN><br>        <SPAN style="color:#00007F">If</SPAN> Target.Cells.Count = 1 <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#007F00">' only act if it is a single cell, so multicell copy will not trigger this</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> InStr(1, Target.Value, "PMT") <SPAN style="color:#00007F">Then</SPAN><br>                Me.Unprotect Password:=""<br>                Target.EntireRow.Locked = <SPAN style="color:#00007F">True</SPAN><br>                Me.Protect Password:=""<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>



<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> LockPMT()<br><SPAN style="color:#007F00">' Lock those rows where Column V contains "PMT"</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> rChk <SPAN style="color:#00007F">As</SPAN> Range, r1st <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> r1st = Columns("V").Find(What:="PMT", _<br>            after:=Cells(Rows.Count, "V"), _<br>            LookIn:=xlValues, lookat:=xlPart, _<br>            searchdirection:=xlNext)<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> r1st <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">' PMT is found</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> rChk = r1st<br>        <SPAN style="color:#00007F">Do</SPAN><br>            ActiveSheet.Unprotect Password:=""<br>            rChk.EntireRow.Locked = <SPAN style="color:#00007F">True</SPAN><br>            ActiveSheet.Protect Password:=""<br>            <SPAN style="color:#00007F">Set</SPAN> rChk = Columns("V").FindNext(after:=rChk)<br>        <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> rChk.Address <> r1st.Address <SPAN style="color:#007F00">' else endless loop</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> r1st = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rChk = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

If you lock with a password, supply it in the code
 
Upvote 0
Hi,

I have a similar problem ,

My value that will be changing is link to another spreadsheet = 1- reject , 2- In progress 3-

I'm trying to have it lock when value contains "1-"

Appreciate the experts advice.
 
Upvote 0
  • Go to the sheet where you want this to happen and right click on the name tab.
  • Select View Code...
  • The macro editor opens in the Sheet Module. This is where 'events' for this sheet can be monitored and acted on.
  • Paste the following macro there:

Code:
Option explicit 

Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Target, Columns("V")) Is Nothing Then
        ' the changed cell is in column V. Modify the "V" to whatever your column is where the "1-" comes in
        If Target.Cells.Count = 1 Then
            ' only act if it is a single cell, so multicell copy will not trigger this
            If InStr(1, Target.Value, "1-") Then
                Me.Unprotect Password:=""
                Target.EntireRow.Locked = True
                Me.Protect Password:=""
            Else            
                Me.Unprotect Password:=""
                Target.EntireRow.Locked = False
                Me.Protect Password:=""
            End If

        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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