• 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.

Prevent Users to run macro

Abhijeet

Active Member
Hi

I want to run macro for listed users name means if particular users open file then run that macro if that user is not in list then that macro disable how to do this
Please tell me how to do this
 
Many ways to do it. Some that comes to mind.

1. Grab environmental variable - Windows/Network login user name and check this against list and exit sub if not found
Code:
Environ("UserName")

2. Instead of checking user name, just ask for password before user can run macro
Code:
Sub pswdProt()
Dim pswd As String

pswd = Inputbox("Enter password", "Password", "?????")
If pswd <> "yourpassword" Then
MsgBox "Incorrect Password"
Exit Sub
Else
End If
'Put your code here
End Sub
 
Well you are only disabling part of your code by calling it on workbook open.

Your code has more than one function.

Modify code to following and test.
Code:
Sub pswdProt()
Dim pswd As String

pswd = Inputbox("Enter password", "Password", "?????")
If pswd <> "yourpassword" Then
MsgBox "Incorrect Password, Macro disabled"
Application.EnableEvents = False
Exit Sub
Else
Application.EnableEvents = True
End If
'Put your code here
End Sub
 
Is this correct way bcoz its not not please tell me how to do this

Code:
Private Sub Workbook_Open()
Dim pswd As String

pswd = InputBox("Enter password", "Password", "?????")
If pswd <> "swami" Then
MsgBox "Incorrect Password, Macro disabled"
Application.EnableEvents = False
Exit Sub
Else
Application.EnableEvents = True
End If
    'Force the current selection to be selected, triggering the appropriate
    'state of the cut, copy & paste commands
    Call ChkSelection(ActiveSheet)
    ThisWorkbook.Application.CellDragAndDrop = False
End Sub
 
Can you upload sample with data that demonstrates how your code behaves without above? It'll be easier for me to trace what each part of your code is doing.

As I don't have time to check your code in it's entirety right now.
 
Users Name in sheet i want to Run Macro this macro if User Name not in list then i want this macro disable
 

Attachments

  • Cut_Copy disable (2).xlsm
    41.3 KB · Views: 7
It seems your macro is broken. I have to manually run "Application.EnableEvents = True" or it returns error message.

When I turn it on... nothing seems to be happening. I think this is variant on copy & paste prevent code I helped you with while back?
 
yes this is Copy paste prevent code i want to just toggle the sheet & selection change then copy mode is false this should happen please give me macro for this
 
Oh, by the way, don't forget to turn on "Application.EnableEvents" to true before you close workbook or another stage. Or else it's going to cause issues.
 
Do you have a clean code that's working for you (cut copy portion at least)? If not I'll demonstrate it on the one I gave you. Just give me time to find the file.
 
Here, try this file. And see how the code is used.
 

Attachments

  • Cut_Copy_Demo.xlsm
    23.4 KB · Views: 7
You never told me where you are getting user name from.

Is it from Windows/Network login? Or is it Excel User? Or other source?
 
Patience. I've got to earn living as well you know ;)

See attached.
 

Attachments

  • Cut_Copy_Demo (1).xlsm
    24 KB · Views: 9
Back
Top