vba 如何使电子表格中的单元格只读?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3560089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:43:41  来源:igfitidea点击:

How to make cells read-only in spreadsheets?

vbaexcel-vbaspreadsheetexcel

提问by gizgok

I'm populating a spreadsheet with Database values. The cells that I fill data with, I want them to be read-only to the users. How can I do this?

我正在用数据库值填充电子表格。我填充数据的单元格,我希望它们对用户是只读的。我怎样才能做到这一点?

采纳答案by JeffO

Depends on how you want to select the range. This is just one cell. If you go back to J1 and change the value, you should get prompted.

取决于您要如何选择范围。这只是一个单元格。如果您返回 J1 并更改值,您应该会收到提示。

Private Sub Worksheet_Change(ByVal Target As Range)
  Range("J1").Select
  Selection.Locked = True
  ActiveSheet.Protect Contents:=True
  Range("K1").Select

End Sub

Cells are not locked until the worksheet is protected. By default all cells are set to Locked, so you'll have to unlock cells you want users to be able to change.

在工作表受到保护之前,单元格不会被锁定。默认情况下,所有单元格都设置为锁定,因此您必须解锁您希望用户能够更改的单元格。

回答by Lunatik

In these circumstances I often find the best way is to lock the sheet, but only for the user by using the UserInterfaceOnlyargument which still allows unrestricted programmatic interaction with the sheet.

在这些情况下,我经常发现最好的方法是锁定工作表,但仅限于用户使用UserInterfaceOnly仍然允许与工作表无限制编程交互的参数。

ActiveSheet.ProtectUser InterfaceOnly:=True

There are various other arguments that can be set that will still allow the user to filter, sort etc. should this be required, the Help file has a full list.

还有各种其他参数可以设置,这些参数仍然允许用户进行过滤、排序等。如果需要,帮助文件有一个完整的列表。

回答by froadie

You can try protecting cells. Or you can code it yourself using a SelectionChange handle event... :

你可以试试保护细胞。或者您可以使用 SelectionChange 句柄事件自己编写代码...:

   Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Not Intersect(Selection, Range("A24:A50")) Is Nothing Then
        Range("B1").Select
      End If
    End Sub

回答by e2-e4

This pageoffers a small sub to protect a range of cells.

此页面提供了一个小子来保护一系列单元格。

Excerpt

摘抄

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    If Not Intersect(Target, Range("H1:H10")) Is Nothing Then 
        Target.Offset(0, 1).Select 
        MsgBox "you may not enter that cell" 
    End If 
End Sub

To protect H1:H10. When the user wants to change a cell within H1:H10a warning message says that the cell may not be modified, and the neighbor cell in the next column (I) is selected instead.

为了保护H1:H10。当用户想要更改H1:H10警告消息中的单元格时,该单元格可能不会被修改,I而是选择下一列 ( ) 中的相邻单元格。

回答by DeaconnFrost

In excel you can go to the Protection menu and specify which cells would require a password to modify. You can specify multiple ranges as well.

在 Excel 中,您可以转到“保护”菜单并指定哪些单元格需要密码才能修改。您也可以指定多个范围。

Hope this is what you were looking for.

希望这就是你要找的。