0

I created a user defined function in ms excel 2010 for fetching current logged in user thru environvariable "USERNAME".

However, on some machines running ms excel2010 on win7, this function does not update current user name.

I had packaged it in a protected worksheet and distributed to several users.

The udf code goes this way : Public Function UserName() UserName = Environ$("UserName") End Function

Then the function is called using the formula:

=Username()

Community
  • 1
  • 1

1 Answers1

1

Consider:

Public Function WhoAmI() As String
   Application.Volatile
   WhoAmI = "The NT Logon User is " & Environ("username")
   WhoAmI = WhoAmI & vbCrLf & "The Office Username is " & Application.UserName
End Function

Should work on any Windows system.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • I do not need application user name. I need only windows username. So can I skip the application.username part. Also, what does application.volatile do. – aliarshad111 Feb 04 '16 at 18:20
  • @user3305648 You can omit the part that you do not need......the `Application.Volatile` forces re-calculation when a different user opens the workbook. – Gary's Student Feb 04 '16 at 18:37
  • Thanks. As I understand, application.recalculate will work only if any cell in a workbook changes. So will it update when a different user opens the workbook. – aliarshad111 Feb 04 '16 at 20:19