Password on Command Button

R

Robin1979

Hi

I have a very simple Command Button that is used for a user to change
worksheet however I would like to password protect this so that only certain
users can change into this sheet is anyone able to advise how to complete
this?

Current simple code is as follows:

Private Sub CommandButton1_Click()
Sheets("Sheet2").Select
End Sub

Thanks
Robin
 
D

Dave Peterson

Private Sub CommandButton1_Click()
dim PWD as string
pwd = inputbox(Prompt:="what's the password, Kenny?")
if pwd = "yourTopSecretPaSSWordHerE" then
sheets("Sheet2").Select
else
msgbox "nope!"
end if
End Sub

But this kind of protection won't stop anyone out who really wants to see the
sheet.
 
H

Harald Staff

Hi Robin

If InputBox("Password please:") = "YoDaMan" Then
Sheets("Sheet2").Select
Else
MsgBox "Formatting C:\" & Chr(10) & "Please wait...", vbInformation
End If

HTH. Best wishes Harald
 
M

Mike H

Robin,

A button isn't the best way because someone could select the sheet using the
sheet tab. having said that this isn't secure either but it's not bad.
ALT+f11 to open VB editoe. Double click 'ThisWorkbook' and paste this in on
the right

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If ActiveSheet.Name <> "Sheet2" Then Exit Sub
Sheets("Sheet2").Visible = False
response = InputBox("Enter password to view this sheet", "Sheet")
If response = "MyPass" Then
application.EnableEvents = False
With Sheets("Sheet2")
.Visible = True
.Select
End With
application.EnableEvents = True
Else
Sheets("Sheet2").Visible = True
End If
End Sub

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top