Results 1 to 4 of 4

VBA GetUserName

Threaded View

  1. #1
    Registered User
    Join Date
    05-04-2011
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    35

    VBA GetUserName

    I have the following code to get user name. I don't know how to add user name to one cell and have it locked and add another user name in another cell and have it locked when they open it to verify. Is it possible?
    Sub Username()
    Function GetName(Optional NameType As String) As String
         'Function purpose:  To return the following names:
         'Defaults to MS Office username if no parameter entered
         '
         'Formula should be entered as =GetName([param])
         '
         'For Name of Type       Enter Text  OR  Enter #
         'MS Office User Name      "Office"        1 (or leave blank)
         'Windows User Name        "Windows"       2
         'Computer Name            "Computer"      3
         
    
         'Force application to recalculate when necessary.  If this
         'function is only called from other VBA procedures, this
         'section can be eliminated. (Req'd for cell use)
        Application.Volatile
         'Set value to Office if no parameter entered
        If Len(NameType) = 0 Then NameType = "OFFICE"
         'Identify parameter, assign result to GetName, and return
         'error if invalid
        Select Case UCase(NameType)
        Case Is = "OFFICE", "1"
            GetName = Application.Username
            Exit Function
        Case Is = "WINDOWS", "2"
            GetName = Environ("UserName")
            Exit Function
        Case Is = "COMPUTER", "3"
            GetName = Environ("ComputerName")
            Exit Function
        Case Else
            GetName = CVErr(xlErrValue)
        End Select
    End Function
    Last edited by Leith Ross; 04-03-2012 at 11:16 PM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1