我需要更新许多旧的.xls文件来锁定和保护单元格块,以防止人们打开单元格并复制出标题信息。这是一次性过程,遗憾的是,我无法阻止\限制对文件的访问。
在兼容模式下使用Excel 2010我拼凑了一个宏来打开文件,锁定单元格,保护工作表,然后保存并关闭文件并转到下一个文件。但是,当我手动重新打开文件时,我仍然可以从锁定的单元格中复制。工作表显示为受保护,当我解锁它们显示为锁定的单元格时,它们会显示选中的锁定单元格。有什么想法吗?
Sub Lockdown()
Dim wBk As Workbook
Dim sFileSpec As String
Dim sPathSpec As String
Dim sFoundFile As String
sPathSpec = "C:\Archive\PF\"
sFileSpec = "*.xls"
sFoundFile = Dir(sPathSpec & sFileSpec)
Do While sFoundFile <> ""
Set wBk = Workbooks.Open(sPathSpec & sFoundFile)
With wBk
Application.DisplayAlerts = False
For Each ws In Worksheets
ws.Unprotect 'Some worksheets have protection without password
ws.Unprotect Password:="A1234" 'some worksheets have password protection
ws.Range("A1:I10").Locked = True
ws.Protect contents:=True
Next
Application.DisplayAlerts = True
End With
Set wBk = Nothing
Workbooks(sFoundFile).Close True
sFoundFile = Dir
Loop
End Sub
感谢您的帮助!
答案 0 :(得分:1)
下面是我放在一起的示例,它将阻止用户仅选择单元格A1,A2,B1和B2。但是,可以选择所有其他单元格。
Set myWkbk = ActiveWorkbook
Set mySht = myWkbk.Worksheets(1)
With mySht
.Cells.Locked = False
.Range("A1:B2").Locked = True
.Protect contents:=True
.EnableSelection = xlUnlockedCells
End With