VBA ENVIRON Function

VBA ENVIRON Function

Objective of VBA Environ Function

VBA Environ function 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 can be used 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)

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

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA Environ function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of Environment Variables

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

Environment Variables

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA Information Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *