How to add username in excel

  • Thread starter Dave VB logic for excel
  • Start date
D

Dave VB logic for excel

Hello,

I hope you guys can help me. Im currently working in excel sheet. I would
like to know tha formula where the shee should capture the username from the
system or the licence which allocated to the user. What is the function or vb
formula?

Thank you in advanced.
 
T

Tom Hutchins

Here are a couple of alternatives. The simplest way is
Dim UserName as string
UserName = Environ("USERNAME")

Here is another way to retrieve the user's LAN ID using an API call. Copy
this code into a general VBA module in your workbook:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Public Function UserName() As String
Dim Buffer As String * 100
Dim BuffLen As Long
On Error GoTo UNerr
BuffLen = 100
GetUserName Buffer, BuffLen
UserName = Left(Buffer, BuffLen - 1)
Exit Function
UNerr:
UserName = vbNullString
End Function

You would call it like this in VBA:
Sub AAAAA()
MsgBox UserName()
End Sub

Or, from a worksheet cell:
=UserName()

Finally, you could also try retrieving Application.UserName, which is the
name entered on the Tools >> Options >> General tab.

If you are new to user-defined functions (macros), this link to Jon
Peltier's site may be helpful:
http://peltiertech.com/WordPress/2008/03/09/how-to-use-someone-elses-macro/

Hope this helps,

Hutch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top