Lee*_*edo 4 security excel vba spreadsheet
我的工作簿有一些非常隐藏的工作表 ( xlSheetVeryHidden),因为我不希望最终用户看到这些工作表中的信息。
\n但是,令我惊讶的是,使用这种方法非常弱,因为任何最终用户都可以从另一个工作簿运行以下代码并查看主文件上的所有隐藏工作表。
Sub make_visible()\n \nDim wb As Workbook: Set wb = Workbooks("Test") \xe2\x80\x98name of the main file\nDim sh As Worksheet\n For Each sh In wb.Sheets\n sh.Visible = xlSheetVisible\nNext\n \nEnd Sub\nRun Code Online (Sandbox Code Playgroud)\n因此,我需要隔离我的工作簿,并防止在其他工作簿上找到的任何宏影响我的主工作簿。
\n感谢所有有用的评论和对此安全问题的回答。
\n另外,我锁定了我的 vba 项目,我使用了类似的方法,如 Unviewable+
您可以保护工作簿结构,这将阻止用户添加/删除/隐藏/取消隐藏工作表。
您可以在 Excel 界面的“审阅”功能区选项卡中手动执行此操作:

或者,您可以通过以下方式以编程方式完成此操作:
'*******************************************************************************
'Protect the Structure of a given Book
'Returns TRUE if the Book is passworded with the provided pass otherwise FALSE
'*******************************************************************************
Public Function ProtectBookStructure(ByVal book As Workbook _
, ByVal pass As String) As Boolean
If book.ProtectStructure Then
If UnprotectBookStructure(book, pass) Then
'Previous password was the same or empty. Protect with new password
ProtectBookStructure = ProtectBookStructure(book, pass)
Else
'Book is protected with a different password
ProtectBookStructure = False
End If
Else
On Error Resume Next
book.Protect Password:=pass, Structure:=True
On Error GoTo 0
ProtectBookStructure = book.ProtectStructure
End If
End Function
'*******************************************************************************
'Unprotect the Structure of a given Book
'Returns TRUE if successful or FALSE if not
'*******************************************************************************
Public Function UnprotectBookStructure(ByVal book As Workbook _
, ByVal pass As String) As Boolean
If book.ProtectStructure Then
On Error Resume Next
book.Unprotect pass
On Error GoTo 0
End If
UnprotectBookStructure = Not book.ProtectStructure
End Function
Run Code Online (Sandbox Code Playgroud)
上述方法可以这样调用:
ProtectBookStructure ThisWorkbook, "simplePassword"
UnprotectBookStructure ThisWorkbook, "simplePassword"
Run Code Online (Sandbox Code Playgroud)
或者您可以利用它们的返回值来检查保护/取消保护是否成功。
阅读了OP问题下的补充评论后,我必须澄清一些事情。
只有通过文件/信息保护 Excel 文件,才能确保它们的安全:

但是,没有密码,任何人都无法使用此类受保护的文件。一旦你给了他们密码,一切就都可以访问了,因为:
.xls然后简单地删除它(不提示输入密码)来轻松“破解”。这同样适用于工作表。此外,暴力破解 VBA 宏可以在旧版本的 Excel 上实现相同的效果。此外,还可以编辑内部文件(Excel 文件存档下)以删除密码DPB=..\xl\vbaProject.bin\PROJECT 下的条目我在这个答案中提出的建议仅阻止“普通”用户取消隐藏隐藏的工作表,但这并不能阻止更多“高级”用户通过宏从这些工作表中复制信息,而且肯定不会阻止他们破解密码。
如果您希望这些隐藏工作表具有完全的安全性,那么您应该按照 @SolarMike 在评论中的建议将数据移到 Excel 文件之外