我想阻止其他人使用VBA编辑Excel表格中的单元格内容。是否有可能做到这一点?如何使用vba在Excel中锁定单元格中的数据
11
A
回答
3
尝试使用Worksheet.Protect
方法,像这样:
Sub ProtectActiveSheet()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Protect DrawingObjects:=True, Contents:=True, _
Scenarios:=True, Password="SamplePassword"
End Sub
你应该,但是,关心包括在你的VBA代码的密码。如果你只是试图建立一个简单的屏障,以防止用户发生像删除公式这样的小错误,你不一定需要密码。
另外,如果你想看看如何做某些事情在Excel中的VBA中,尝试录制一个宏并查看它生成的代码。这是开始使用VBA的好方法。
21
你可以先选择你不想被他们的锁定状态设置为false来保护(是用户可编辑),该细胞:
Worksheets("Sheet1").Range("B2:C3").Locked = False
然后,您可以保护工作表,所有其他细胞将受到保护。 的代码要做到这一点,而且还让你的VBA代码修改的细胞是:
Worksheets("Sheet1").Protect UserInterfaceOnly:=True
或
Call Worksheets("Sheet1").Protect(UserInterfaceOnly:=True)
0
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
0
你也可以做到在工作表的捕获的工作水平改变事件。如果你的需求更符合你的需求。允许基于价值观,标准,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
2
比方说,例如,在一种情况下,如果从范围A1要锁定的细胞然后在下面I50是代码:
Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"
在另一种情况下,如果你已经有了那么保护工作表按照以下代码:
ActiveSheet.Unprotect Password:="Enter your Password"
Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"
相关问题
- 1. 在vba excel中锁定单元格
- 2. 如何锁定excel中的单元格
- 3. vba excel锁定所有单元格
- 4. 在VBA中锁定单元格
- 5. 在Excel VBA中打印特定单元格中的数据
- 6. 锁定Excel中的特定单元格VBA
- 7. 如何在Excel中处理这个单元格锁? (VBA)
- 8. 在Excel中动态锁定单元格
- 9. 锁定单元格小于零和空单元格VBA Excel
- 10. 锁定/解锁单元excel VBA
- 11. 锁定工作表中的所有单元格Excel VBA
- 12. Excel VBA锁列包含单元格中的特定值
- 13. 如何在导出到Excel时锁定SSRS中的单元格?
- 14. 使用VBA在Excel中锁定行
- 15. 锁定Excel单元格中的单元格值
- 16. 如何使用VBA在Excel中设置特定单元格中的公式?
- 17. 使用officewriter在excel中可以锁定单元格吗?
- 18. 使用Excel VBA连接特定单元格数据
- 19. 如何使用Excel VBA计算单元格中特定字符的数量
- 20. 如何使用多个密码锁定Excel中的单个单元格?
- 21. Excel(VBA,Userform)如何将Listbox中的数据添加到特定的单元格?
- 22. 如何计算excel中使用VBA的注释单元格?
- 23. 如何删除空行的Excel单元格中使用VBA
- 24. 定位单元格的值在Excel VBA
- 25. 如何锁定Excel工作表中的单元格内容
- 26. excel中的VBA格式单元格
- 27. Excel - 如何尽可能不使用宏来锁定单元格
- 28. VBA Excel行中的单元格中断
- 29. 在excel中使用VBA转置循环中的单元格
- 30. 使用excel VBA在Excel单元格中粘贴excel公式(excel格式)?
http://stackoverflow.com/questions/17111648/how-to-lock-excel-cells-in-vba/ 18868676#18868676 – 2013-09-18 09:39:09