Reputation: 611
My workbook has some very hidden sheets (xlSheetVeryHidden
), because I do not want the end users to even see the info inside this sheets.
But, I surprised that using this method is very weak ,because any end user can run the below code from another workbook and see all the hidden sheets on the main file.
Sub make_visible()
Dim wb As Workbook: Set wb = Workbooks("Test") ‘name of the main file
Dim sh As Worksheet
For Each sh In wb.Sheets
sh.Visible = xlSheetVisible
Next
End Sub
So, I need to isolated my workbook and prevent any macro found on other workbook to affect my main workbook.
Grateful for all useful comments and answer for this security issue.
Also,I locked my vba project , I have used a similar method like Unviewable+
Upvotes: 4
Views: 769
Reputation: 4558
You could protect the workbook structure which would prevent the user to add/delete/hide/unhide sheets.
You can do it manually in the Excel interface in the Review ribbon tab:
Or, you can do it programatically with something like:
'*******************************************************************************
'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
The above methods can be called like:
ProtectBookStructure ThisWorkbook, "simplePassword"
UnprotectBookStructure ThisWorkbook, "simplePassword"
or you could make use of their return value to check if the protect/unprotect is succesful.
Having read the additional comments made under the OP's question, I must clarify a few things.
Excel files can only be made safe if you protect them via File/Info:
However, nobody can use such a protected file without the password. And once you give them the password then everything is accessible because:
.xls
format and then simply removing it (no prompt for password). Same is applicable for Worksheets. Also, a brute force VBA macro could achive the same thing on older versions of Excel. Also, the internal files (under the Excel file archive) can be edited to remove the passwordsDPB=..
entry under \xl\vbaProject.bin\PROJECTThe suggestion I made in this answer only prevents 'normal' users to unhide the hidden worksheets but that does not stop more 'advanced' users to copy the information from those sheets via macros and most certainly does not prevent them to crack the passwords.
If you want total security on those hidden sheets then you should move the data outside of the Excel file as suggested by @SolarMike in the comments
Upvotes: 5