简体   繁体   English

如何锁定包含公式的单元格中的数据,以便无法使用VBA在excel中对其进行编辑

[英]How to Lock the data in a cell containing formulas so that they cannot be edited in excel using vba

I am in a very strange situation in VBA. 我在VBA中处于非常奇怪的情况。 I am new to VBA and struggling since a couple of days in solving this problem. 解决问题的几天以来,我对VBA还是陌生的。

I want to Lock the top three rows and the first three columns of my excel sheet for editing. 我想锁定Excel工作表的前三行和前三列进行编辑。 However, these cells contain formulas, which I don't want the end user to edit and make changes. 但是,这些单元格包含公式,我不希望最终用户编辑和进行更改。 So basically, what I need is, the cells should perform all the operations, but should not be edited. 因此,基本上,我需要的是,单元应该执行所有操作,但不应该对其进行编辑。 How can, I do it in VBA. 我怎么能在VBA中做到这一点。 I am using Excel 2010. 我正在使用Excel 2010。

One more thing, I have a button in the top rows ($B$2:$C$3) occupying four cells. 还有一件事,我在顶部的行中有一个按钮($B$2:$C$3)占据四个单元格。 This button is assigned to a macro which does the job of clearing contents in all the cells except the cell I Intend to lock. 此按钮分配给一个宏,该宏负责清除除我要锁定的单元格之外的所有其他单元格中的内容。 In short, in the other cells, the user can export data from a notepad and thereafter the formulas in the locked cell will do the job. 简而言之,在其他单元格中,用户可以从记事本中导出数据,此后,锁定的单元格中的公式将起作用。 When the user has to export new set of datas, he will cick on the button which is in ($B$2:$C$3) , which clears the existing data and the user can imprt new set of dat from the notepad. 当用户必须导出新数据集时,他将单击($B$2:$C$3)的按钮,这将清除现有数据,并且用户可以从记事本中插入新的数据集。

When I use 当我使用

Worksheets("Sheet1").Range("B2:C3").Locked = False

Worksheets("Sheet1").Protect UserInterfaceOnly:=True 

my button in ($B$2:$C$3) is not working properly. 我的($B$2:$C$3)按钮无法正常工作。 Any help from anybody will behighly solicited. 任何人的任何帮助都将受到高度重视。 Thanks a lot in advance. 非常感谢。

try this: 尝试这个:

Worksheets("Sheet1").Range("B2:C3").Locked = true

Worksheets("Sheet1").Protect UserInterfaceOnly:=True 

This should fix the problem. 这应该可以解决问题。

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

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