VBA ENVIRON Function

VBA ENVIRON Function in Excel

VBA Environ function in Excel is categorized as an Information function in VBA. It is a built-in function in MS Office Excel VBA. This function returns value of the current operating system environment variable. It has one input parameter. It returns a string value. We can use this function only in VBA not as Excel function.

This function use in either procedure or function in a VBA editor window in Excel. We can use this VBA Environ function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Environ function, where we can use this VBA Environ function and real-time examples.

Syntax of VBA Environ Function

The syntax of the VBA Environ function is

Environ(Expression)

Note: The VBA Environ function returns a string value.

Parameters or Arguments

The Environ function has one input parameter or argument.
Where
Expression: It is a required parameter. The Expression is an argument that represents numeric number or variable name in the environment variable/string table.

Where we can apply or use VBA Environ Function?

We can use this VBA Environ function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Find computer name using the Environ function

Here is a simple example of the VBA Environ function. This below example checks the for the computer name using the Environ function. The output of below macro is ‘GT’.

'Find computer name using the Environ function
Sub VBA_Environ_Function_Ex1()
    
    'Variable declaration
    Dim iExpression As String
    Dim sOutput As String
    
    iExpression = "ComputerName"
      
    sOutput = Environ(iExpression)
   
    'Display output message
    MsgBox "Computer Name : " & sOutput, vbInformation, "VBA Environ Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA Environ Function

Example 2: Find User name using the Environ function

Here is a another example of the VBA Environ function. This below example checks the for the User name using the Environ function. The output of below macro is

'Find User name using the Environ function
Sub VBA_Environ_Function_Ex2()
    
    'Variable declaration
    Dim iExpression As String
    Dim sOutput As String
    
    iExpression = "UserName"
      
    sOutput = Environ(iExpression)
   
    'Display output message
    MsgBox "User Name : " & sOutput, vbInformation, "VBA Environ Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA Environ Function

Example 3: Find User Profile using the Environ function

Here is a example of the VBA Environ function. This below example checks the for the User Profile using the Environ function. The output of below macro is ‘PNRao’.

'Find UserProfile using the Environ function
Sub VBA_Environ_Function_Ex3()
    
    'Variable declaration
    Dim iExpression As String
    Dim sOutput As String
    
    iExpression = "UserProfile"
      
    sOutput = Environ(iExpression)
   
    'Display output message
    MsgBox "User Profile : " & sOutput, vbInformation, "VBA Environ Function"
    
End Sub

Output:Here is the screen shot of the third example output.
VBA Environ Function

Example 4: Find Home Drive using the Environ function

Here is one more example of the VBA Environ function. This below example checks the for the Home Drive using the Environ function. The output of below macro is ‘C:\Users\PNRao’.

'Find Home Drive using the Environ function
Sub VBA_Environ_Function_Ex4()
    
    'Variable declaration
    Dim iExpression As String
    Dim sOutput As String
    
    iExpression = "HomeDrive"
      
    sOutput = Environ(iExpression)
   
    'Display output message
    MsgBox "Home Drive : " & sOutput, vbInformation, "VBA Environ Function"
    
End Sub

Output:Here is the screen shot of the fourth example output.
VBA Environ Function

Example 5: Find numeric value of 4 an Environment variable

Here is a another example of the VBA Environ function. This below example finds for the numeric value of 4 an Environment variable using the Environ function. The output of below macro is ‘CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files’.

'Find numeric value of 4 an Environment variable
Sub VBA_Environ_Function_Ex5()
    
    'Variable declaration
    Dim iExpression As Integer
    Dim sOutput As String
      
    sOutput = Environ(4)
   
    'Display output message
    MsgBox "The Numeric value of 4 an Environment variable : " & sOutput, vbInformation, "VBA Environ Function"
    
End Sub

Output:Here is the screen shot of the fifth example output.
VBA Environ Function

List of Environment Variables

Click on below link to get more details about the Environment Variables.

Environment Variables

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply