How to Use the VBA Environ Function (4 Examples)

The Environ function is one of the most important and widely used functions of VBA. In this article, I’ll show you how you can use the Environ function with proper examples and illustrations.


VBA Environ Function (Quick View)

Quick View of the VBA Environ Function

When you run it, it’ll return CommonProgramFiles(x86)=C:\ProgramFiles(x86)\Common Files, because the 4th system environment variable of the operating system is CommonProgramFiles.


Download Practice Workbook

Download this practice workbook to exercise while you are reading this article.


An Introduction to the VBA Environ Function

⧭ Overview:

The Environ function takes a number as the input and returns the system environment variable of the computer of that numeric position.

For example, Environ(1) = CommonProgramFiles(x86)=C:\ProgramFiles(x86)\Common Files

Also, you can enter the name of the environment variable within the Environ function. It’ll return the value of the variable.

For example, Environ(“CommonProgramFiles”) = C:\ProgramFiles(x86)\Common Files

⧭ Syntax:

Syntax of the VBA Environ Function

Therefore, the syntax of the Environ function is:

=Environ(Expression)

⧭ Arguments:

Argument Required / Optional Explanation
Expression Required The numeric position of the environment variable that’ll be returned. Or the name of the environment value whose value will be returned.

⧭ Return Value: 

Returns an environment variable, or the value of an environment value.


4 Examples to Use the VBA Environ Function

Here are a few examples to learn to use the VBA Environ function in detail.


Example 1: Showing the System Environment Variable of a Particular Numeric Position

This is the main purpose of the Environ function. That is, to display the system environment variable of a particular variable.

Let’s find out the 4th system environment variable of our operating system.

The VBA code will be:

⧭ VBA Code:

Sub VBA_Environ_Function()

MsgBox Environ(4)

End Sub

Quick View of the VBA Environ Function

⧭ Output:

Run the Macro. it’ll display the 4th system environment variable of the operating system. It’s CommonProgramFiles(x86)=C:\ProgramFiles(x86)\Common.

Read More: How to Use VBA IsNumeric Function (9 Examples)


Example 2: Finding Out the Components of a Specific System Variable

This is also one of the common usages of the VBA Environ function. Let’s try to see the components of the system variable CommonProgramFiles.

The VBA code will be:

⧭ VBA Code:

Sub VBA_Environ_Function()

MsgBox Environ("CommonProgramFiles")

End Sub

VBA Code to Use the VBA Environ Function

⧭ Output:

Run the code, and it’ll show the components of the system variable CommonProgramFiles of the operating system.

Output to Use the VBA Environ Function

Related Content: How to Use VBA Val Function in Excel (7 Examples)


Similar Readings:


Example 3: Checking whether a Particular Program is Added to the Path Variable of Your Operating System or Not

You can also check whether a particular program is added to the Path variable of your operating system or not using the VBA Environ function.

Let’s check whether the program WindowsPowerShell is added or not.

The VBA code will be:

⧭ VBA Code:

Sub VBA_Environ_Function()

Path = Environ("Path")

For i = 1 To Len(Path)
    If Mid(Path, i, 17) = "WindowsPowerShell" Then
        MsgBox "The Program is Added."
        Exit For
    End If
Next i

End Sub

VBA Code to Use the VBA Environ Function

⧭ Output:

Run the code. It’ll display “The Program is Added” on my computer. Because the program “WindowsPowerShell” is added to my operating system.

Read More: How to Use VBA MkDir Function in Excel (6 Examples)


Example 4: Counting the Number of Components of a Particular System Variable with the VBA Environ Function

You can also count the number of components of a particular system variable with the Environ function.

The VBA code will be:

Let’s count the number of components of the Path variable of the operating system.

The VBA code will be:

⧭ VBA Code:

Sub VBA_Environ_Function()

Path = Environ("Path")

Count = 0

For i = 1 To Len(Path)
    If Mid(Path, i, 1) = ";" Then
        Count = Count + 1
    End If
Next i

MsgBox Count

End Sub

VBA Code to Use the VBA Environ Function

⧭ Output:

Run the code. It’ll display 7 on my computer. Because the number of components added to my Path variable is 7.

Read More: Excel Formula to Generate Random Number (5 examples)


Things to Remember

Here I’ve used the Mid function of VBA within some of my codes to access a group of characters from my system variables. Click here to learn the Mid function in detail.


Conclusion

Using these methods, you can use the Environ function of VBA. Do you have any questions? Feel free to ask us. And don’t forget to visit our website ExcelDemy for more posts and updates.


Related Articles

Rifat Hassan

Rifat Hassan

Hello! Welcome to my profile. Here I will be posting articles related to Microsoft Excel. I am a passionate Electrical Engineer holding a Bachelor’s degree in Electrical and Electronic Engineering from the Bangladesh University of Engineering and Technology. Besides academic studies, I always love to keep pace with the revolution in technology that the world is rushing towards day by day. I am diligent, career-oriented, and ready to cherish knowledge throughout my life.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo