• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

password to run macro

kalpeshpatel

New Member
it is possible to create a password to run macro


mean


when i click command button to run a macro to ask password for run which restricted other person to use macro
 
Sure thing. Here's an example:

[pre]
Code:
Private Const MyPass as String = "abc123"
Sub MyMacro()
Dim UserInput As Variant
UserInput = InputBox("Please enter the password", "Password")
If UserInput <> MyPass Then
MsgBox "Invalid Password"
Exit Sub
End If

'Do something

End Sub
[/pre]
 
Hi kalpeshpatel


maybe something like this?


Code:
Sub Macro1()

[code]Dim Password

[code]Password = InputBox("Enter your password:", "Password", "********")

[code]If Password = "password" Then

[code]MsgBox "Access available", vbInformation, "Access"

' there is rest of the procedure

[code]Exit Sub

Else[/code]

MsgBox "Wrong password!", vbCritical, "Error"[/code]

Exit Sub[/code]

End If[/code]

End Sub[/code]


Hope this helps.

Best regards

slaya_cz
 
Hi, kalpeshpatel!


In addition to any of the two previous posts I'd suggest to protect the VBA code with another different password, in order to prevent that curious users give a look to the code and discover the password.


A less secure option is to replace the constant against which check the password (MyPass in Luke M's example and "password" in slaya_cz's one) to something like this:


-----

Dim MyPass as string

MyPass=chr$(XX)&chr$(XX)&chr$(XX)&chr$(XX)&chr$(XX)...

-----


Choosing the adequate XX values for each chr$() function, you can build the same password and not write it in plain text. It's not fully safe, but it's safer than the explicit method.


Regards!
 
Back
Top