Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Password Protect a Macro 1

Status
Not open for further replies.

olympus

Technical User
Apr 16, 2002
26
US
I would like to password protect some, not all, of my macros and code modules. I have already protected my VB code from being changed by setting the "VBA-Project- Project properties" and then selected the protection tab, lock project for viewing and added a password. I want to prevent a user from pressing alt+f11 and then selecting, Tools, Macro and then running a macro from the list. I've searched this forum and others without any luck. I do not want to disable the alt+f11 keyboard function as I have disabled all of the menus, right-click short cut menu,and tool bars in the workbook. Therefore I do not know how to enable them again if I cannot go throught the alt+f11 command and then run a macro from the tools menu. Any help/suggestions would be appreciated. Thanks.
John
 
olympus,

In VB Editor View Project, right click the Project Object & select VBA Project Properties - Protection Tab.

You can LOCK from viewing with a password.

:)

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Skip, I have already done that. However one can still run macros from the main menu in VB by selecting tools, macro and then running the macro of one's choice. I was hoping to prevent the user from running any macros or modules in this manner. Thanks.
John
 
Well, put you own password routine at the beginning of any macro that you don't want run without authorization.

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
The way to "hide" macros from appearing in the Tools/Macros menu is to declare an argument (we used to call these things parameters). By making the argument optional, it doesn't need to be specified when calling from somewhere in code.

Put this in a module and experiment:
Code:
Sub ThisShows()
  MsgBox "I can be run from the menu"
End Sub

Sub ThisDoesntShow(Optional Dummy As Integer = 0)
  MsgBox "I can't be run from the menu"
End Sub

Sub RunThisDoesntShow()
[green]' For demo only[/green]
  ThisDoesntShow
End Sub

The other way is to declare the sub as Private. But if you need to run it from some other module, then that way won't work for you.
Code:
Private Sub ThisIsPrivate()
  MsgBox "This is private"
End Sub

Sub RunThisIsPrivate()
[green]' For demo only[/green]
  ThisIsPrivate
End Sub
 
Skip, As I am new to VB please give me an example of a "password routine" to place at the begining of my macro. Thanks.
John
 
John,

You can do is simply with a InputBox,but the data entered can be VIEWED over someone's shoulder.

Or you can use a Control Toolbox Textbox control and set the PasswordChar property.

But here's a sample...
Code:
If Textbox1.Text <> "qwerty" then exit sub
or you can give the user a message. This one just exits if the entered value is not the authorized value. And there's only ONE AUTHORIZED VALUE.

Other considerations:
Will each user have their own password?
How will passwords be securely stored?
How will passwords be maintained?

It can get quite involved.

:)

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Skip, thank you. Your sample solved it for me. A star to you as my thanks.
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top