简体   繁体   English

遍历范围内的单元格并根据其值锁定单元格

[英]Loop through cells in a range and lock cells based on their value

OK say i have a range of cells from A1:B10. 好吧,我有一系列来自A1:B10的单元格。 The cells in the range A1 to A10 contain a dropdown list with 2 options (lock, dont_lock). A1到A10范围内的单元格包含一个带有2个选项(锁定,dont_lock)的下拉列表。 The cells in the range B1 to B10 are empty cells which allow the user to enter data. B1到B10范围内的单元格是空单元格,允许用户输入数据。 what i want to do is lock individual cells based on values in the adjacent cell. 我想做的是基于相邻单元格中的值锁定单个单元格。 so if cell A1 is set to "lock" then i lock cell B1. 因此,如果单元格A1设置为“锁定”,那么我将锁定单元格B1。 IF cell A2 is set to "dont_lock" then B2 is not locked and so on. 如果单元格A2设置为“ dont_lock”,则B2未锁定,依此类推。 I have tried using a for loop shown in my code below but it does not work. 我尝试使用下面的代码中显示的for循环,但是它不起作用。 Can Anyone help ? 有人可以帮忙吗?

Dim rCell As Range
Dim rRng As Range

Set rRng = Sheet1.Range("A1:B10")

For Each rCell In rRng.Cells
    If rCell = "Lock" Then
    Range("B1").Locked = True

End If

Next rCell

In Excel, cells are locked by default. 在Excel中,默认情况下单元格是锁定的。 If you want cells to be unlocked you have to actually specify that behavior. 如果要解锁单元,则必须实际指定该行为。 However, it doesn't matter if the cell is locked or not if the worksheet isn't currently protected. 但是,如果当前未保护工作表,则是否锁定单元格都没有关系。 So if you want the locked behavior, make sure to call Sheet1.Protect . 因此,如果要锁定行为,请确保调用Sheet1.Protect

Now if I understand your explanation correctly you would do better with something like this: 现在,如果我正确理解了您的解释,那么使用以下方法会更好:

Dim rCell As Range
Dim rRng As Range

Set rRng = Sheet1.Range("A1:A10") 'Exclude B as you are only checking A.
Sheet1.Unprotect
For Each rCell In rRng.Cells
    'Avoid if else structure since locking depends on result of evaluation only.
    rcell.Offset(0,1).locked = rcell.Value = "Lock"
Next rCell
Sheet1.Protect

Try the following code. 请尝试以下代码。 Only issue with your code is that rCell.value = "Lock" is missing. 您的代码唯一的问题是缺少rCell.value =“ Lock”。 You are only locking Cell B1 for all cells, you are not locking the adjacent all cells :) 您只为所有单元锁定单元格B1,没有锁定相邻的所有单元格:)

** THIS IS A SAMPLE CODE** **这是示例代码**

Option Explicit

Sub lockNextCell()
Dim wkSheet As Worksheet
Dim rng As Range, rCell As Range

Set wkSheet = Sheets("Sheets1") '-- use your own sheet
Set rng = wkSheet.Range("A3:A12") '-- use your own range

For Each rCell In rng
    If rCell.Offset(0, 0).Value = "Lock" Then
        rCell.Offset(0, 1).Locked = True '-- here we are locking the adjacent cell
    else
        rCell.Offset(0, 1).Locked = False
    End If
Next rCell

End Sub

Just a note as Daniel talks about protecting sheet: 丹尼尔(Daniel)谈到保护床单时,只需要注意:

Here is a reference from MSDN : "If you lock a cell and protect the worksheet, then you cannot type data into the cell, modify the data currently in the cell, or change other attributes of the cell (such as cell formatting)" 这是MSDN参考 :“如果锁定单元格并保护工作表,则不能在单元格中键入数据,修改单元格中当前的数据或更改单元格的其他属性(例如,单元格格式)”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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