简体   繁体   English

如何使用vba锁定excel中单元格中的数据

[英]How to Lock the data in a cell in excel using vba

I want to stop others from editing the cell contents in my excel sheet using VBA. 我想阻止其他人使用VBA编辑我的Excel工作表中的单元格内容。 Is it possible to do this? 是否有可能做到这一点?

You can first choose which cells you don't want to be protected (to be user-editable) by setting the Locked status of them to False: 您可以通过将它们的锁定状态设置为False来首先选择您不希望受保护的单元格(用户可编辑):

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

Then, you can protect the sheet, and all the other cells will be protected. 然后,您可以保护工作表,并保护所有其他单元格。 The code to do this, and still allow your VBA code to modify the cells is: 执行此操作的代码仍然允许您的VBA代码修改单元格:

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

or 要么

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

Try using the Worksheet.Protect method, like so: 尝试使用Worksheet.Protect方法,如下所示:

Sub ProtectActiveSheet()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    ws.Protect DrawingObjects:=True, Contents:=True, _
        Scenarios:=True, Password="SamplePassword"
End Sub

You should, however, be concerned about including the password in your VBA code. 但是,您应该担心在VBA代码中包含密码。 You don't necessarily need a password if you're only trying to put up a simple barrier that keeps a user from making small mistakes like deleting formulas, etc. 如果您只想设置一个简单的屏障来防止用户犯下删除公式等错误,那么您不一定需要密码。

Also, if you want to see how to do certain things in VBA in Excel, try recording a Macro and looking at the code it generates. 此外,如果您想在Excel中查看如何在VBA中执行某些操作,请尝试录制宏并查看它生成的代码。 That's a good way to get started in VBA. 这是开始使用VBA的好方法。

Let's say for example in one case, if you want to locked cells from range A1 to I50 then below is the code: 比方说,例如,在一种情况下,如果要锁定从A1到I50的单元格,那么下面是代码:

Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"

In another case if you already have a protected sheet then follow below code: 在另一种情况下,如果您已经有受保护的工作表,请按照以下代码:

ActiveSheet.Unprotect Password:="Enter your Password"
Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"
Sub LockCells()

Range("A1:A1").Select

Selection.Locked = True

Selection.FormulaHidden = False

ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= False, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:=True

End Sub

You can also do it on the worksheet level captured in the worksheet's change event. 您也可以在工作表的更改事件中捕获的工作表级别上执行此操作。 If that suites your needs better. 如果能满足您的需求。 Allows for dynamic locking based on values, criteria, ect... 允许基于值,标准等动态锁定...

Private Sub Worksheet_Change(ByVal Target As Range)

    'set your criteria here
    If Target.Column = 1 Then

        'must disable events if you change the sheet as it will
        'continually trigger the change event
        Application.EnableEvents = False
        Application.Undo
        Application.EnableEvents = True

        MsgBox "You cannot do that!"
    End If
End Sub

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

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