+ Reply to Thread
Results 1 to 4 of 4

VBA GetUserName

Hybrid 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

  2. #2
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: VBA GetUserName

    Hi Kinez,

    I am not sure if I really understood, what you need. Still I have modified your macro to get it working, and hopefully help you understand what it does:

    Sub Username()
    Dim PW As String
    
    PW = "Pass"  'Change for your Password
    
    ActiveSheet.Unprotect PW
    
    'Write Description in column B
    ActiveSheet.Range("b1").Value = "MS Office User Name"
    ActiveSheet.Range("b2").Value = "Windows User Name"
    ActiveSheet.Range("b3").Value = "Computer Name"
    
    'Retrieve the Username (three different options)
    ActiveSheet.Range("c1").Value = GetName(1)
    ActiveSheet.Range("c2").Value = GetName(2)
    ActiveSheet.Range("c3").Value = GetName(3)
    ActiveSheet.Protect PW
    
    End Sub
    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
    Do you want to use the username to check if a particular user is allowed to open a file? If yes we would simply need to call the function from the Workbook Open Macro. Let me know if that's what you want, and if you need my (or other Forum members) help on this.

    Regards

    Theo
    Remember To Do the Following....
    1. Upload sample files
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VBA GetUserName

    I'm not sure what you are doing but this might help
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

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

    Re: VBA GetUserName

    Solved. I used UserForm at the start up and by clicking few things I was able to solve the problem.

+ Reply to Thread

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