VBA: Problems with Environ("Username")?

Peter h

Active Member
Joined
Dec 8, 2015
Messages
409
I have recently started using Environ("Username") quite a bit. I use it to track who's using my workbook, or track issues certain users may be having, and also to make a few of my userform functions a bit more user friendly. I've been having these strange issues with my workbook that I had not previously been able to really track down or fix, and only recently tied these issues to my uses with the Environ commands I was using. So, I decided to go back to the basics and remove all of my code that I was using the Environ command, except for error tracking purposes. Well, I came into work this morning and was going over any of the issues that may have popped up since last week, and noticed that on one of the errors it tracked it had recorded the wrong username for the time stamp that it had recorded. There was another one where it didn't record a username at all. So, something dawned on me, and I was able to come up with a theory as to why I was having an issue when using the Environ command. I think what is going on is that every once in a while a user forgets to log off of the computer, so it automatically locks, and then the next user logs in. So then there are 2 people logged into the same computer (1 active and 1 inactive).

- Would this cause confusion in my code when Excel tries to determine what Environ("Username") to use for a certain command?
- Is there a way to force excel to use the active username and ignore the inactive one?
- Any thoughts or suggestions for me?
- Is the Environ command known to be a bit finicky?
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,886
Try this:

Code:
Function CurrentUser()
Set objNetwork = CreateObject("Wscript.Network")
CurrentUser = objNetwork.username
End Function
 

Forum statistics

Threads
1,082,629
Messages
5,366,645
Members
400,908
Latest member
currong

Some videos you may like

This Week's Hot Topics

Top