如何在VBA中锁定Excel单元格? [英] How to lock Excel cells in VBA?

查看:1182
本文介绍了如何在VBA中锁定Excel单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel工作表,像一个应用程序,表单控制按钮允许用户导航通过记录。首先,上一个,下一个&最后一个周期适当地通过其中一个工作表记录,在我的表单工作表中显示值。



当用户不在编辑或添加模式时,我想锁定单元格以防止用户修改内容。



我尝试过Range(A1:O24)。Locked = True,但我仍然可以在细胞。



任何人都知道如何完成这个?我需要我的vba代码,以便在用户导航时能够为单元格分配新的值,但是除非在添加或编辑模式下,否则禁止用户输入新值。

解决方案

我相信原因是您需要在单元格实际锁定之前保护工作表。所有单元格的格式都被锁定为默认值,所以您真正想要做的是将不要锁定的范围设置为Range()。Locked = False,然后将工作表设置为protected。



如果您希望将所有单元格锁定,则必须将工作表设置为受保护的


I have an Excel worksheet that acts like an application, with form control buttons allowing users to 'navigate' through records. First, Previous, Next & Last cycle appropriately through one of the worksheets records, displaying the values in my 'form' worksheet.

When users are not in Edit or Add Mode, I would like to lock the cells to prevent users from modifying contents.

I tried Range("A1:O24").Locked = True, but I am still able to type new values into the cells.

Anyone know how to accomplish this? I need my vba code to be able to assign new values to the cells as users 'navigate', but to prevent users from entering new values unless in Add or Edit mode.

解决方案

I believe the reason for this is that you need to protect a worksheet before cells actually become locked. All cells are formatted as locked as a default so what you really want to do is set the range that you don't want locked to Range().Locked = False and then set the worksheet to protected.

In the case that you want all cells locked all you have to do is set the worksheet to protected

这篇关于如何在VBA中锁定Excel单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆