Excel VBA - Understanding and Using the "Environment"

brothertruffle880
brothertruffle880 used Ask the Experts™
on
Aside from username, which other environment items can I display using the following:

I'm asking because I just want to see what environ can do for me.


Sub test()

MsgBox "hello " & Environ("username"), vbOKOnly, "Have a Great Day"

End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
President / Owner CARDA Consultants Inc.
Distinguished Expert 2018
Commented:
These are the variables that seem to exist on my PC

 1            ALLUSERSPROFILE
 2            APPDATA
 3            CommonProgramFiles
 4            CommonProgramFiles(x86)
 5            CommonProgramW6432
 6            COMPUTERNAME
 7            ComSpec
 8            configsetroot
 9            DriverData
 10           FPS_BROWSER_APP_PROFILE_STRING
 11           FPS_BROWSER_USER_PROFILE_STRING
 12           HOMEDRIVE
 13           HOMEPATH
 14           LOCALAPPDATA
 15           LOGONSERVER
 16           NUMBER_OF_PROCESSORS
 17           OneDrive
 18           OneDriveConsumer
 19           OS
 20           Path
 21           PATHEXT
 22           PROCESSOR_ARCHITECTURE
 23           PROCESSOR_ARCHITEW6432
 24           PROCESSOR_IDENTIFIER
 25           PROCESSOR_LEVEL
 26           PROCESSOR_REVISION
 27           ProgramData
 28           ProgramFiles
 29           ProgramFiles(x86)
 30           ProgramW6432
 31           PSModulePath
 32           PUBLIC
 33           SESSIONNAME
 34           SystemDrive
 35           SystemRoot
 36           TEMP
 37           TMP
 38           USERDOMAIN
 39           USERDOMAIN_ROAMINGPROFILE
 40           USERNAME
 41           USERPROFILE
 42           windir

That said, you can check for yourself by using
Sub AllEnvironVariables()
    Dim strEnviron            As String
    Dim VarSplit              As Variant
    Dim i                     As Long
    
    For i = 1 To 255
        strEnviron = Environ$(i)
        If LenB(strEnviron) = 0& Then GoTo TryNext:
        VarSplit = Split(strEnviron, "=")
        If UBound(VarSplit) > 1 Then Stop
        Debug.Print i, VarSplit(0) ', VarSplit(1)
TryNext:
    Next
End Sub

Open in new window

which is a slight variation of the code found at https://wellsr.com/vba/2019/excel/list-all-environment-variables-with-vba-environ/

Do remember Environment variables are not secure and can be spoofed, so they shouldn't be used for anything security related!

Typically, the useful ones are

COMPUTERNAME
SystemDrive
SystemRoot
TMP
USERPROFILE
windir

Author

Commented:
Thanks so much for the detailed list. I'm demonstrating the environment variables in VBA and your list was helpful.  How did you obtain the list.  I did a HELP search and couldn't find the listing you provided.
Daniel PineaultPresident / Owner CARDA Consultants Inc.
Distinguished Expert 2018

Commented:
I simply used the function I included in my reply.  It generated the listing based on my PC's environment variables.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial