In a standard module add this code:
Option Explicit
Sub HideDataSheet()
Dim Answer$
' If Data sheet is visible then hide it
If Sheets("Data").Visible = True Then
' Select the button
ActiveSheet.Shapes("Button 1").Select
' Change the text on the button to reflect the status of the button
Selection.Characters.Text = "Unhide Sheet"
' Hide the Data sheet
Sheets("Data").Visible = xlVeryHidden
' Select cell A1
Range("A1").Select
' Show a message that the sheet is hidden
MsgBox "Data sheet is now hidden.", vbInformation
Else
ResumeHere:
' If Data sheet is hidden then ask for a password
Answer = InputBox("Password", "Please enter administrative password.")
If Answer = "Hello" Then
' Unhide the data sheet if the password is correct
Sheets("Data").Visible = xlSheetVisible
' Select the button
ActiveSheet.Shapes("Button 1").Select
' Change the text on the button to reflect the status of the button
Selection.Characters.Text = "Hide Sheet"
' Select cell A1
Range("A1").Select
' Show a message that the sheet is hidden
MsgBox "Data sheet is now visible.", vbInformation
Else
' If nothing is entered or the InputBox is cancelled then exit the sub
If Len(Trim(Answer)) = 0 Then Exit Sub
' If password is incorrect then show a message
If MsgBox("Password is incorrect." & vbCrLf & "Would you like to try again?", vbOKCancel) = vbCancel Then Exit Sub Else GoTo ResumeHere
End If
End If
End Sub
Then add a button and link the button to this subroutine.
Don't forget to add a password to the vba project or anyone can just open and change the code.
It's a bit long but it checks several possible scenarios and gives message boxes along the way to guide the end user.
Hope this helps.
abousetta
Bookmarks