简体   繁体   English

锁定某个范围内的某些单元格

[英]Lock certain cells in a range

I'm trying to loop through a range of cells, locking any cell that has content while leaving empty cells unlocked. 我正在尝试循环一系列单元格,锁定任何具有内容的单元格,同时保持空单元格未锁定。

When I run the below code the result is the entire sheet is locked. 当我运行以下代码时,结果是整个工作表被锁定。 If I add an else statement the sheet is unlocked. 如果我添加一个else语句,表单将被解锁。 Basically whatever the last .locked = (true, false) statement is is how the entire sheet winds up. 基本上无论最后一个.locked =(true,false)语句是整个工作表如何结束。

Change 1 Is it possible that I have some setting on/off that is interfering since I'm the only one who is unable to get any of this to work? 更改1我是否有可能因为我是唯一一个无法使用此功能而开启/关闭的设置?

Sub ProtectTheSheet()
Dim chCell As Range
Dim chRng As Range

'Clear the default status
ActiveSheet.Unprotect
Range("A7:I35").Locked = False

Set chRng = ActiveSheet.Range("A7:I35")

'Check cell value in body and lock cells with content
For Each chCell In chRng.Cells
    If chCell.Value <> "" Then Cells.Locked = True
Next chCell

ActiveSheet.Protect

End Sub
Sub ProtectTheSheet()
    Dim chCell As Range
    Dim chRng As Range

    ActiveSheet.Unprotect
    Set chRng = ActiveSheet.Range("A7:I35")

    'Check cell value in body and lock cells with content
    For Each chCell In chRng.Cells
        chCell.Locked = (chCell.Value <> "")
    Next chCell

    ActiveSheet.Protect

End Sub

Check this out: http://www.mrexcel.com/archive/VBA/15950b.html 看看这个: http//www.mrexcel.com/archive/VBA/15950b.html

Sub CellLocker()
Cells.Select
' unlock all the cells
Selection.Locked = false
' next, select the cells (or range) that you want to make read only, 
' here I used simply A1
Range("A1").Select
' lock those cells
Selection.Locked = true
' now we need to protect the sheet to restrict access to the cells. 
' I protected only the contents you can add whatever you want
ActiveSheet.Protect DrawingObjects:=false, Contents:=true, Scenarios:=false
End Sub

If you say Range("A1").Select, then it locks only A1. 如果你说Range(“A1”)。选择,那么它只锁定A1。 You can specify multiple cells to be locked by specifying as follows: 您可以通过指定如下指定要锁定的多个单元格:
A3:A12,D3:E12,J1:R13,W18 A3:A12,D3:E12,J1:R13,W18
This locks A3 to A12 and D3 to E12 etc. 这将A3锁定到A12和D3到E12等。

I know this is an old thread, but I've been stuck on this for a while too, and after some testing on Excel 2013 here's what I conclude if your range includes any merged cell 我知道这是一个老线程,但我也一直坚持这一点,在Excel 2013上进行一些测试后,如果你的范围包括任何合并的单元格,我会得出结论

  • The merged cells must be entirely included within that range (eg the merging must be entirely within the range being lock/unlocked 合并的单元格必须完全包含在该范围内(例如,合并必须完全在锁定/解锁的范围内
  • The range being merged can be larger, or at least exactly the range corresponding to the merged cells. 被合并的范围可以更大,或者至少恰好是与合并的单元相对应的范围。 If it's a named range that works as well. 如果它是一个有效的命名范围。

Also, you cannot lock/unlock a cell that is already within a protected range. 此外,您无法锁定/解锁已受保护范围内的单元格。 Eg if you run: 例如,如果您运行:

public sub test()
   Sheet1.range("myNameRange").locked = true
   Sheet1.protect
end sub

Twice it will work the first time, and fail the second time around. 两次它将第一次工作,第二次失败。 So you should unprotect the target range (or the sheet) before.... 所以你应该在......之前取消保护目标范围(或表格)。

You can try this. 你可以试试这个。

Public Sub abc()
ActiveSheet.Unprotect Password:="1234"
ActiveSheet.Range("I8:I500, K8:K500, M8:M500, N8:N500").Cells.Locked = False
ActiveSheet.Protect Password:="1234"
End Sub

If you want to protect the specific cells of any specific excel without the password protection then here is the solution: 如果你想在没有密码保护的情况下保护任何特定excel的特定单元格,那么这里是解决方案:

Sub ProtectingSheet()

  Workbooks.Open (c\documents\....)

  Dim mainworkBook As Workbook

  Set mainworkBook = ActiveWorkbook

  Worksheets(CellValue).Activate

  mainworkBook.Sheets("Sheet1").Range("A1:AA100").Locked = True

  Range(Cells(1, 2), Cells(1, 25)).Select
  Selection.Locked = False

  ActiveSheet.Protect

End Sub

I may be missing something but... 我可能会遗漏一些东西,但......

Cells.Locked = True

...will lock all cells on the active sheet. ...将锁定活动工作表上的所有单元格。 If you just change it to... 如果你只是改为......

chCell.Locked = True

...then it works; ......然后它起作用; I think?! 我认为?! As the range is very small, you may as well not unlock cells at the start, and instead unlock cells whilst locking them eg 由于范围非常小,您也可以在开始时不解锁细胞,而是在锁定它们时解锁细胞,例如

For Each chCell In chRng.Cells
    If chCell.Value <> "" Then 
    chCell.Locked = True
    Else
    chCell.Locked = False
    End If
Next chCell

If you are new to VBA, I would recommend cycling through code line-by-line as described in this Excel consultant's video . 如果您不熟悉VBA,我建议您按照此Excel顾问的视频中所述逐行循环访问代码。 If you step through code, you can check "has cell A7 behaved as expected?"...instead of just seeing the end product 如果您单步执行代码,您可以检查“是否有单元格A7按预期运行?”...而不是仅仅查看最终产品

A quick way to unlock non-blank cells is to use SpecialCells see below. 解锁非空白单元格的快捷方法是使用SpecialCells,如下所示。

On my testing this code handles merged cells ok , I think this is what is generating your error on Tim's code when it looks to handle each cell individually (which to be clear is not an issue in Tim's code, it is dealing with an unexpected outcome) 在我的测试中,这段代码可以处理合并的单元格 ,我认为这是Tim的代码在单独处理每个单元格时会产生错误的原因(明确的是Tim的代码中没有问题,它处理意外的结果) )

You may also find this article of mine A fast method for determining the unlocked cell range useful 您也可以在我这篇文章中找到一种确定解锁单元格范围的快速方法

Sub Quicktest()
    Dim rng1 As Range
    Dim rng2 As Range
    On Error Resume Next
    Set rng1 = ActiveSheet.Range("A7:I35").Cells.SpecialCells(xlFormulas)
    Set rng2 = ActiveSheet.Range("A7:I35").Cells.SpecialCells(xlConstants)
    On Error GoTo 0
    ActiveSheet.Unprotect
    ActiveSheet.Range("A7:I35").Cells.Locked = False
    If Not rng1 Is Nothing Then rng1.Cells.Locked = True
    If Not rng2 Is Nothing Then rng2.Cells.Locked = True
    ActiveSheet.Protect
End Sub

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

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