[英]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.很抱歉,这个问题可能看起来像另一个问题,但我是 VBA 的新手,很难获得正确的代码......我想在验证后保护一些数据。 For exemple, I have data in Column B (or whatever column) and I check them in comparaison to manuscrit raw data.
例如,我在 B 列(或任何列)中有数据,我将它们与 manuscrit 原始数据进行比较。 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...)我找到了锁定整行的代码( 使用 VBA 锁定 Excel 中的行),但无论我尝试什么,我都无法修改它以适用于变量整列(仅锁定特定列,我无法锁定输入“是”的列...)
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.这设置为检查工作表的第 3 行是否为是。 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.第 1 步:select 工作表中的所有单元格。 Goto properties (cell format) -> security and deaktivate the checkbox for lock cells (see picture below).
转到属性(单元格格式)-> 安全性并取消激活锁定单元格的复选框(见下图)。
Step 2: protect the sheet with a password.第2步:用密码保护工作表。 In my example I used "billytalent".
在我的示例中,我使用了“billytalent”。
Step 3: copy the following code into the code-area of the sheet.第 3 步:将以下代码复制到工作表的代码区域。 Therefore open the Visual Basic Editor.
因此打开 Visual Basic 编辑器。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.