Code Samples for Businesses, Schools & Developers

First Published 3 Oct 2022

The Environ function returns the value of operating system environment variables.

Environ(numeric_value) OR Environ(variable_name)

a)   Numeric_value
      Environ(1): "ALLUSERSPROFILE=C:\ProgramData"
      Environ(2): "APPDATA=C:\Users\username\AppData\Roaming"
      Environ(3): "CommonProgramFiles=C:\Program Files (x86)\Common Files"

b)   Variable_name
      Environ("ALLUSERSPROFILE"): "C:\ProgramData"
      Environ("APPDATA"): "C:\Users\username\AppData\Roaming"
      Environ("CommonProgramFiles"): C:\Program Files (x86)\Common Files"

A typical system may have over 40 environment variables.
The following code lists all environment variables to the immediate window in the visual basic editor

Copy the code to a standard module


Sub ListEnviron()

'lists all envionment variables
      Dim strg As String
      Dim x As Long

      strg = "Environ Values 1 to 50" & vbCrLf & _
            "===================" & vbCrLf
      For x = 1 To 50
            strg = strg & x & " " & Environ(x) & vbCrLf
      Next x

      Debug.Print strg

End Sub

I hope you find this information useful

Colin Riddington           Mendip Data Systems                 Last Updated 3 Oct 2022

Return to Code Samples Page

Return to Top