锁定某个范围内的某些单元格
我正在尝试循环访问一系列单元格,锁定任何包含内容的单元格,同时解锁空单元格。
当我运行下面的代码时,结果是整个工作表被锁定。如果我添加 else 语句,工作表将被解锁。基本上,无论最后一个 .locked = (true, false) 语句是什么,整个工作表都是如何结束的。
更改 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
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. Basically whatever the last .locked = (true, false) statement is is how the entire sheet winds up.
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?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以试试这个。
You can try this.
看看这个:http://www.mrexcel.com/archive/VBA/15950b.html
如果你说 Range("A1").Select,那么它只锁定 A1。您可以通过如下指定来指定要锁定的多个单元格:
A3:A12,D3:E12,J1:R13,W18
这会将 A3 锁定到 A12,将 D3 锁定到 E12 等。
Check this out: http://www.mrexcel.com/archive/VBA/15950b.html
If you say Range("A1").Select, then it locks only A1. You can specify multiple cells to be locked by specifying as follows:
A3:A12,D3:E12,J1:R13,W18
This locks A3 to A12 and D3 to E12 etc.
我可能遗漏了一些东西,但是...
...将锁定活动工作表上的所有单元格。如果你只是将其更改为...
...那么它就可以工作;我认为?!由于范围非常小,您最好不要在开始时解锁单元格,而是在锁定单元格的同时解锁单元格,例如,
如果您是 VBA 新手,我建议您按此 Excel 顾问的视频。如果您单步执行代码,您可以检查“单元格 A7 的行为是否符合预期?”...而不仅仅是查看最终产品
I may be missing something but...
...will lock all cells on the active sheet. If you just change it to...
...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 e.g.
If you are new to VBA, I would recommend cycling through code line-by-line as described in this Excel consultant's video. If you step through code, you can check "has cell A7 behaved as expected?"...instead of just seeing the end product
解锁非空白单元格的一种快速方法是使用 SpecialCells,如下所示。
在我的测试中,此代码可以正常处理合并单元格,我认为这就是在蒂姆的代码中单独处理每个单元格时产生错误的原因(需要明确的是,这在蒂姆的代码中不是问题,它正在处理一个意想不到的结果)
你也可能会发现我的这篇文章一种确定解锁单元格范围的快速方法有用
A quick way to unlock non-blank cells is to use SpecialCells see below.
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)
You may also find this article of mine A fast method for determining the unlocked cell range useful
我知道这是一个旧线程,但我也被困在这个问题上有一段时间了,经过对 Excel 2013 的一些测试,如果您的范围包含任何合并单元格,我得出的结论是
此外,您也无法锁定/解锁。已经在受保护范围内的单元格,例如,如果您运行:
两次,第一次会起作用,第二次会失败,因此您应该在...之前取消保护目标范围(或工作表)。
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
Also, you cannot lock/unlock a cell that is already within a protected range. E.g if you run:
Twice it will work the first time, and fail the second time around. So you should unprotect the target range (or the sheet) before....
如果您想在没有密码保护的情况下保护任何特定 Excel 的特定单元格,那么解决方案如下:
If you want to protect the specific cells of any specific excel without the password protection then here is the solution: