How To Use Function for returning the user name using VBA in Excel?
Tags: tousefunctionforretu, vbamacros
Published: 2021-09-30 08:08:59

---
title: How To Use Function for returning the user name using VBA in Excel?
published: true
date: 2021-09-29 04:44:03 UTC
tags: ToUseFunctionForRetu,VBAMacros
canonical_url: https://geekexcel.com/how-to-use-function-for-returning-the-user-name-using-vba-in-excel/
---

In this tutorial, we will guide you on how to **use a function for returning the username using VBA** in Excel. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Example

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

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

Function ReturnUserName() As String
' returns the NT Domain User Name
Dim rString As String * 255, sLen As Long, tString As String
    tString = ""
    On Error Resume Next
    sLen = GetUserName(rString, 255)
    sLen = InStr(1, rString, Chr(0))
    If sLen > 0 Then
        tString = Left(rString, sLen - 1)
    Else
        tString = rString
    End If
    On Error GoTo 0
    ReturnUserName = UCase(Trim(tString))
End Function

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-Code-91.png)<figcaption>Save the Code</figcaption>

- If you don’t want to **return the network username** , but want to **return the username that the user has registered** with the application, you can use the code given below.

```
ActiveUserName = Application.UserName

```

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.
- Finally, this **function** will **return the username for the logged on user** , works in both **Windows NT and Windows95/98**.

## Wrap-Up

We hope that this tutorial gives you guidelines on how to **use a function for returning the username using VBA** in Excel. ** ** Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Keep Reading:**

- **[How to Modify and Manage Multiple Cell Comments in Excel Office 365?](https://geekexcel.com/how-to-modify-and-manage-multiple-cell-comments-in-excel-office-365/)**
- **[Remove/Add Author Name in Comments in Excel 365](https://geekexcel.com/how-to-remove-add-author-name-in-comments-in-excel-365/)**
- **[How to Create User Defined Function through Excel VBA?](https://geekexcel.com/how-to-create-user-defined-function-through-excel-vba/)**
- **[Extract the Missing Values through Excel VBA](https://geekexcel.com/how-to-extract-the-missing-values-through-excel-vba/)**
- **[How to use Loan Calculator through Excel Office VBA?](https://geekexcel.com/how-to-use-loan-calculator-through-excel-office-vba/)**