Search code examples
excelspreadsheetpassword-protectionvba

Protect a sheet from opening within a workbook


I have an excel workbook with multiple sheets. Is there any way to password protect users from even opening a sheet within the workbook? The sheet has a large graph on it that I don't want everyone to be able to see, let alone edit.

Thanks in advance for your help

EDIT

I used the following code to allow users to click a formcontrol button to access the sheet in question.

Sub ShowHeatMap()
 Dim S As String
 S = InputBox("Enter Password")
 If S = vbNullString Then
     Exit Sub
 ElseIf S <> "wiretransfer" Then
     Exit Sub
 Else
     Worksheets("Training Heat Map").Visible = xlSheetVisible
 End If
End Sub

This is associated with my button on a kind of "homepage" sheet that I added to the workbook.

But I can't get the sheet to remain hidden when you open the workbook again. I tried this code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 ThisWorkbook.Worksheets("Training Heat Map").Visible = xlSheetVeryHidden
End Sub

Any ideas? This code is inputted on the module for the sheet under general declarations


Solution

  • Via this answer, I think this might do the trick for you. Within VBA, put this within ThisWorkbook:

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim MySheets As String, Response As String
    MySheet = "Sheet1"
    If ActiveSheet.Name = MySheet Then
    ActiveSheet.Visible = False
        Response = InputBox("Enter password to view sheet")
            If Response = "MyPass" Then
                Sheets(MySheet).Visible = True
                Application.EnableEvents = False
                Sheets(MySheet).Select
                Application.EnableEvents = True
            End If
    End If
    Sheets(MySheet).Visible = True
    End Sub
    

    Obviously will require a bit of tailoring to your needs. Remember that you will also need to password protect your VBA code, otherwise anyone will be able to view it and find out the password.