Any ideas how to amend this code

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,046
I have been lucky enough to have been given this code for me which works perfectly well for one use, I need to amend it to work for 2 users, i.e the code needs to work when only the 2 named users log on

If Environ("UserName") <> "John Smith" Then
MsgBox "Sorry, you can't run this macro."
Exit Sub
Else
' Your code goes here
End If


Any ideas ?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If Environ("UserName") <> "John Smith" and Environ("UserName") <> "Jim Bob" Then
MsgBox "Sorry, you can't run this macro."
Exit Sub
Else
' Your code goes here
End If
 
Upvote 0
Pauljj

Can we see your actual code?

What Oorang has posted should work as far as I can see.
 
Upvote 0
This is not the final code, just a test, before I use it properly

Sub OptionButton5_Click()

if environ("username") <> "PAUL HAILES" and
If Environ("username") <> "WENDY BLAINE" Then

MsgBox "Wrong user"
Exit Sub
Else

MsgBox Application.UserName

End If


End Sub
 
Upvote 0
Why do you have the If on 2 lines?
Code:
Sub OptionButton5_Click() 

If environ("username") <> "PAUL HAILES" And  Environ("username") <> "WENDY BLAINE" Then 

     MsgBox "Wrong user" 
     Exit Sub 
Else 

     MsgBox Application.UserName 

End If 

End Sub
 
Upvote 0
...well I still get the same message, I have removed the 'IF' but still get a syntax error..

Sub OptionButton5_Click()

If environ("username") <> "PAUL HAILES" And
environ("username") <> "WENDY BLAINE" Then
MsgBox "Wrong user"
Exit Sub
Else

MsgBox Application.UserName

End If


End Sub
 
Upvote 0
Again I'll ask why is the If on 2 lines?

In the posted code by Oorang and I it's on 1 line.
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top