Add Error Message To Sheet Password

Pavertheway

New Member
Joined
Nov 5, 2018
Messages
47
Hi,

I currently use the following code to lock a sheet in a Workbook:

Code:
mysheet = "Sheet1"If ActiveSheet.Name = mysheet Then
ActiveSheet.Visible = False
    Response = InputBox("Enter password to view sheet")
        If Response = "Sheet1" Then
            Sheets(mysheet).Visible = True
            Application.EnableEvents = False
            Sheets(mysheet).Select
            Application.EnableEvents = True
            End If
End If
Sheets(mysheet).Visible = True
When the password is entered incorrectly, I would like to add a simple message stating "Password entered incorrectly" - how would I do this?

Thanks as always.
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,192
Office Version
2007
Platform
Windows
Try this

Code:
Sub test()
  Dim mysheet As String, Response As Variant
  mysheet = "Sheet1"
  If ActiveSheet.Name = mysheet Then
    ActiveSheet.Visible = False
    Response = InputBox("Enter password to view sheet")
    If Response = "Sheet1" Then
      Sheets(mysheet).Visible = True
      Application.EnableEvents = False
      Sheets(mysheet).Select
      Application.EnableEvents = True
    Else
      MsgBox "Password entered incorrectly"
    End If
  End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,102,369
Messages
5,486,450
Members
407,547
Latest member
Sankarasrinivas

This Week's Hot Topics

Top