简体   繁体   English

如何动态锁定和解锁 Excel 中的单元格?

[英]How to dynamically lock and unlock a cell in Excel?

What should i do to dynamically lock/unlock my cell in excel?我应该怎么做才能动态锁定/解锁 excel 中的单元格? For example, if i create a new document, by default all cells are unlock but i entered a data on that cell it will be lock.例如,如果我创建一个新文档,默认情况下所有单元格都是解锁的,但我在该单元格上输入了一个数据,它将被锁定。 I tried this, which i found here Lock empty cells and unlock free cells我试过了,我在这里找到了Lock empty cells and unlock free cells

Sub test()
    Dim rngTemp As Range

    For Each rngTemp In Range("A1:XFD1048576").Cells
        With rngTemp
            If .Value > 0 Or Len(.Value) > 0 Then
                .Locked = False
            End If
        End With
    Next
End Sub

but it's not working on my case.但它不适用于我的情况。 I am using 2007 excel version.我使用的是 2007 excel 版本。 Do i still need to save the code or Alt + Q is enough?我还需要保存代码还是Alt + Q就足够了?


EDIT: As per @JvdV's answer I tried the following:编辑:根据@JvdV 的回答,我尝试了以下方法:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
With Sheet1
    .Unprotect
    .Cells.Locked = True
    .Cells.SpecialCells(xlCellTypeBlanks).Locked = False
    .Protect
End With
End Sub

But this returns an error Run-time error '1004' No cells were found on .Cells.SpecialCells(xlCellTypeBlanks).Locked = False .但这会返回错误Run-time error '1004' No cells were found on .Cells.SpecialCells(xlCellTypeBlanks).Locked = False

If you really are intested in those cells, you can simply refer to a worksheet's cells.如果您真的对这些单元格感兴趣,您可以简单地参考工作表的单元格。 Also, no need to loop through those cells individually, for example:此外,无需单独遍历这些单元格,例如:

Sub test()

Dim rng As Range
With Sheet1 'Change according to your sheet's CodeName
    .Unprotect
    .Cells.Locked = False
    .Cells.SpecialCells(xlCellTypeBlanks).Locked = True
    .Protect
End With

End Sub

Where .Cells.Locked = False unlocks all cells and .Cells.SpecialCells(xlCellTypeBlanks).Locked = True locks all cells blank cells (Note: a ="" value through formulas is considered a value and will stay unlocked)其中.Cells.Locked = False解锁所有单元格和.Cells.SpecialCells(xlCellTypeBlanks).Locked = True锁定所有单元格空白单元格(注意:公式中的=""值被视为一个值,并将保持解锁状态)

Both Unprotect and Protect are needed to have full effect of your changes. UnprotectProtect都需要使您的更改完全生效。

If this is code you want to run each time a value is changed, you'll have to look into the Worksheet_Change event.如果这是每次更改值时要运行的代码,则必须查看Worksheet_Change事件。 And if your goal is to have empty cells unlocked and cells that contain a value locked, just swap around the True and False .如果您的目标是解锁空单元格并锁定包含值的单元格,只需交换TrueFalse


EDIT (as per your comments)编辑(根据您的评论)

If this is something you like to run on every next selection of cells, try the following (error handler included since you not using the whole worksheet nomore)如果这是您希望在每个下一个单元格选择上运行的东西,请尝试以下操作(包括错误处理程序,因为您不再使用整个工作表)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Sheet1.Unprotect 'Change according to your sheet's CodeName
With Target
    .Cells.Locked = True
    On Error Resume Next
    .Cells.SpecialCells(xlCellTypeBlanks).Locked = False
    On Error GoTo 0
End With
Sheet1.Protect

End Sub

If you looking for an alternative where you loop through your target range, you can implement the suggestion by @M.Schalk如果您正在寻找循环遍历目标范围的替代方法,您可以实施@M.Schalk 的建议

As an addition to the (correct) answer above, here is my suggestion for a Worksheet_Change event, as you requested in the comments.作为上述(正确)答案的补充,这是我对Worksheet_Change事件的建议,正如您在评论中所要求的那样。 This will have to be placed in the workbook-specific code module:这必须放在特定于工作簿的代码模块中:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cll As Range
On Error Resume Next

For Each cll In Target.Cells
    With cll
        If .Value2 <> vbNullString Then
            .Locked = True
        Else
            .Locked = False
        End If
    End With
Next
End Sub

It's important to note, that (at least in my version of Excel) the .Locked property of a cell only has an effect when the sheet is protected.需要注意的是,(至少在我的 Excel 版本中)单元格的.Locked属性仅在工作表受到保护时才有效。 To change the value of the .Locked property however, the sheet must not be protected.但是,要更改.Locked属性的值,工作表必须不受保护。 To incorporate this you might want to use something like this:要合并这一点,您可能需要使用以下内容:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cll As Range
On Error GoTo Handler

Me.Unprotect
For Each cll In Target.Cells
    With cll
        If .Value2 <> vbNullString Then
            MsgBox cll.Value2
            .Locked = True
        Else
            MsgBox "NullString"
            .Locked = False
        End If
    End With
Next
Handler:
    Me.Protect
End Sub

This will lead to every cell becoming un-changeable once a value is entered, while still letting the user enter values in all empty cells.这将导致每个单元格在输入值后变得不可更改,同时仍允许用户在所有空单元格中输入值。 To change existing values you will need to manually unprotect the sheet.要更改现有值,您需要手动取消对工作表的保护。 You might use something like the code provided in the answer above to restore a desired state after the sheet was unprotected and changed.在工作表未受保护和更改后,您可以使用类似于上述答案中提供的代码来恢复所需的 state。

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

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