Get Windows user name

matthewh

New Member
Joined
Mar 13, 2002
Messages
34
Is there a way I can get the name of the user logged onto Windows to use in Excel VBA to name files, put the username into macros and formulas, etc. I have many uses for this kind of functionality in Excel and (if not too off topic) Access as well. I'm sure I've seen this elsewhere before, but can't remember where or how. We use Office 2000 on Win98 and 2000 workstations.

Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi,

Try modificated Code

Code:
Sub Cell()
'originally coded as VB script by A.Vials, converted to VBA by Sly
Dim objInfo
Dim strLDAP
Dim strFullName
  
Set objInfo = CreateObject("ADSystemInfo")
strLDAP = objInfo.UserName
Set objInfo = Nothing
strFullName = GetUserName(strLDAP)
  
Range("A1") = strFullName '<== Adjust cell Reference
  
End Sub
 
Function GetUserName(strLDAP)
  Dim objUser
  Dim strName
  Dim arrLDAP
  Dim intIdx
  
  On Error Resume Next
  strName = ""
  Set objUser = GetObject("LDAP://" & strLDAP)
  If Err.Number = 0 Then
    strName = objUser.Get("givenName") & Chr(32) & objUser.Get("sn")
  End If
  If Err.Number <> 0 Then
    arrLDAP = Split(strLDAP, ",")
    For intIdx = 0 To UBound(arrLDAP)
      If UCase(Left(arrLDAP(intIdx), 3)) = "CN=" Then
        strName = Trim(Mid(arrLDAP(intIdx), 4))
      End If
    Next
  End If
  Set objUser = Nothing
  
  GetUserName = strName
  
End Function

Biz
 
Upvote 0
Here is my take on this, a practical example, by no means refined but it gets the job done.

Code:
Sub init()
' Used to store the domain name and username information
    Dim strUserDomainAndName As String

    strUserDomainAndName = Environ("UserDomain") & "\" & Environ("UserName")

' Begin inputing date information
    Range("A3").Select
' Go to the last row
    Selection.End(xlDown).Select
    Selection.Offset(1, 0).Select
' Insert formula
    ActiveCell.FormulaR1C1 = strUserDomainAndName

' Begin inputing date information
    Range("B3").Select
' Go to the last row
    Selection.End(xlDown).Select
    Selection.Offset(1, 0).Select
' Insert formula
    ActiveCell.FormulaR1C1 = "=NOW()"
' Copy and paste values
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
' Disable marching ants around copied range
    Application.CutCopyMode = False

' Offset to the right
    Selection.Offset(0, 1).Select
End Sub
 
Upvote 0
Hello,
If I want to display a username in a message box how do I add that to VBA?

I am currently using this to get the message box to pop up if a cell needs a value:

Private Sub Worksheet_Deactivate()
Dim rngCheck As Range
Dim cel As Range
Dim j As String
Dim i As Integer
Dim Ws As Worksheet

Set Ws = Sheets("RATE ")
Set rngCheck = Ws.Range("B8:B11")

i = 0
For Each cel In rngCheck
If IsEmpty(cel) Then
i = i + 1
j = j & cel.Address & vbNewLine
End If
Next cel

If i = 0 Then Exit Sub

Ws.Activate
MsgBox "Sorry, you must enter a value!"
End Sub

**Goal is to have it say "Sorry, USERNAME you must enter a value!"
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,044
Members
448,543
Latest member
MartinLarkin

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