Macro button that asks for password before proceeding?

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
 
J

Jacob Skaria

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
 
M

Munchkin

How & where do I specify what the password is? I tried entering the password
inside the quotation marks, but it didn't work.
 
J

Jacob Skaria

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
 
M

Munchkin

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?
 
J

Jacob Skaria

Dim varPassword As Variant
varPassword = Trim(InputBox("Enter Password"))
If Trim(varPassword) <> "mypassword" Then Exit Sub

If this post helps click Yes
 
J

jason

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.
 

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