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)
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:
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
⧭ 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
⧭ Output:
Run the code, and it’ll show the components of the system variable CommonProgramFiles of the operating system.
Related Content: How to Use VBA Val Function in Excel (7 Examples)
Similar Readings:
- Excel VBA ASC() Function – Get ASCII Value of Character
- How to Create a Body Mass Index (BMI) Calculator in Excel Using VBA
- Use TRIM Function in VBA in Excel (Definition + VBA Code)
- How to Use VBA SPLIT Function in Excel (5 Examples)
- How to Use InStr Function in VBA (3 Examples)
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
⧭ 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
⧭ 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.