Macro button that asks for password before proceeding?

  • Thread starter Thread starter Munchkin
  • Start date Start date
M

Munchkin

Is there a way to stop the user and have the prompted to enter a password
before this macro button is run? I was this to only be an option for
Administrative purposes only.

Sheets("POLICY LIST").Select
Rows("1:1").Select
Selection.EntireRow.Hidden = False
Rows("2:6").Select
Selection.EntireRow.Hidden = True
End Sub
 
Sub Macro()

Dim strPassword As String
Do While Trim(strPassword) = ""
strPassword = Trim(InputBox("Enter Password"))
Loop

Sheets("POLICY LIST").Select
Rows("1:1").Select
Selection.EntireRow.Hidden = False
Rows("2:6").Select
Selection.EntireRow.Hidden = True
End Sub

If this post helps click Yes
 
How & where do I specify what the password is? I tried entering the password
inside the quotation marks, but it didn't work.
 
Try the below...password is ........ mypassword

Sub Macro()

Dim strPassword As String
Do While Trim(strPassword) <> "mypassword"
strPassword = Trim(InputBox("Enter Password"))
Loop

Sheets("POLICY LIST").Select
Rows("1:1").Select
Selection.EntireRow.Hidden = False
Rows("2:6").Select
Selection.EntireRow.Hidden = True
End Sub

If this post helps click Yes
 
It works! Thanks so much. Now - for another question. I tested it to see
what happens if you enter the wrong password and it seems to be stuck in
limbo until you enter the correct password. How do I get the cancel button
to end the macro?
 
Dim varPassword As Variant
varPassword = Trim(InputBox("Enter Password"))
If Trim(varPassword) <> "mypassword" Then Exit Sub

If this post helps click Yes
 
Dim varPassword As Variant
varPassword = Trim(InputBox("Enter Password"))
If Trim(varPassword) <> "mypassword" Then Exit Sub

If this post helps click Yes

you could also put a conditional loop that gives back a message as in


do while...

if varpasswors<>"mypassword" then
msgbox("password incorrect")
end if

loop


if you don't want to loop you could just write:

varpw=trim(inputbox("pw please"))

if strcomp(varpw ,"mypassword",vbTextCompare) <>0 then

Workbooks("Your WB").Close SaveChanges:=False
else

your code...

end if


avoids the do while loop which is sometimes a little glitchy for some.
 
Back
Top