简体   繁体   中英

VBA - How to lock entire column based on a cell value in this column

I'm sorry this question may look like antoher ones, but I am new in VBA and struggle to get a proper code... I would like to protect some data after they were verified. For exemple, I have data in Column B (or whatever column) and I check them in comparaison to manuscrit raw data. I would like to have a cell in that column where I say "Yes" to testify this was checked. After entering "Yes", I would like that all cells in the colum become locked.

I've found a code to lock an entire row ( Lock rows in Excel using VBA ), but whatever i try, i'm not able to modifiy it to work for a variable entire column (only to lock specific column, I'm not able to lock the column where "Yes" is typed...)

Could someone help me? Thanks !

You can tweak the code you're looking at there a little and I think it will do what you want. Make sure before you start this that you set all the cells on the sheet to unprotected.

This is set to check row 3 of the sheet for Yes. Change the value on the second line if you need to.

Private Sub Worksheet_Change(ByVal Target As Range)
    row_to_check = 3                                                        ' Checking row 3 for "Yes"
    If Intersect(Target, Me.Rows(row_to_check)) Is Nothing Then Exit Sub    ' exit early if row 3 wasn't changed
    Me.Unprotect                                                            ' unprotect the sheet
        For Each r In Target.EntireColumn.Columns                           ' cycle through each row that has seen a change
              r.Locked = r.Cells(row_to_check, 1).Value = "Yes"             ' set it to protected if the second cell on that row is "Yes"
        Next
    Me.Protect                                                              ' reprotect the sheet
End Sub

Maybe this solution can help:

Step 1: select all cells in a sheet. Goto properties (cell format) -> security and deaktivate the checkbox for lock cells (see picture below).

Step 2: protect the sheet with a password. In my example I used "billytalent".

Step 3: copy the following code into the code-area of the sheet. Therefore open the Visual Basic Editor. On the left side you will find a list with your sheets. Double click on the sheet where you want to lock cells with entry "yes". Copy the procedure into the code-area.

Private Const PASSW As String = "billytalent"

Private Sub Worksheet_Change(ByVal Target As Range)

   Dim cell As Range

   For Each cell In Target
       'only do something, if input is in row 2
       If cell.Row = 2 Then
           'only do something, if someone write yes in a cell
           If cell.Value = "yes" Then

               'deaktivate the protection of the sheet
               ActiveSheet.Unprotect Password:=PASSW

               'lock the cells in the column
               ActiveSheet.Columns(cell.Column).Locked = True

               'activate the protection
               ActiveSheet.Protect Password:=PASSW, userinterfaceonly:=True, AllowFiltering:=True, AllowFormattingCells:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True
               ActiveSheet.EnableOutlining = True

           End If
        End If

   Next

End Sub

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM