Enter Password to run a macro

  • Thread starter Thread starter DJ
  • Start date Start date
D

DJ

How do I make a user enter a password when they select a
cell before a macro can be run. In other words when they
select the cell I want them to have to enter a password to
make them stop and think do I really want to run this
macro because it recalculates the spreadsheet. Now they
are clicking on it by mistake and then instead of closing
with out saving they are saving and then have to reenter
all of their previous data.

Thanks so much.
 
Hi
something quite simple:
sub foo()
Dim pwd as string
pwd = inputbox ("Enter your password", "Attention")
If pwd <>"mypassword" then
msgbox "Wrong password, macro execution stopped"
exit sub
end if

'your code

end sub
 
Hey

I tried this but for some reason it is not working for
me....am I supposed to put it directly above the code for
the macro?? or is it to be by itself? It keeps saying
expected end sub....sorry but I am not used to working
with this much and it may be simple to you but it is not
for me...Thanks.
 
Hi
this should be at the beginning of your macro (that is the first lines
of code). I inserted a comment ('your code) to start you on code. It
should be within the same sub
Note: Of course you can make a separate function with this code and
call this function in all your macros. Something like

Public Function check_for_pwd(pwd_check as string) as boolean
sub foo()
Dim pwd as string
pwd = inputbox ("Enter your password", "Attention")
If pwd <> pwd_check then
check_for_pwd = FALSE
else
check_for_pwd = TRUE
end if
end function

Now in your own sub use this function as follows
Sub your_sub ()
'your declarations

If not check_for_pwd("mypassword") then
msgbox "Wrong Password, execution stopped"
exit sub
end if

'now your own code
end sub
 
Back
Top