Adjusting a Path Based on System and User

Written by Allen Wyatt (last updated September 19, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


3

Walter has a macro that he would like to run on different computers. The macro accesses a file whose name is the same on the different computers but the path to that file may be different. He wonders how he can, within the macro, identify the computer where the macro is running as well as the user running the macro so that the running macro can select the correct path to the file.

Perhaps the easiest way to discover this information is to use the Environ function, in VBA, to return environmental information from the operating system. Consider, for example, the following two lines:

sComputer = Environ("ComputerName")
sUser = Environ("UserName")

After running these two code lines, sComputer will contain the name of the computer system (as defined within Windows) and sUser the name of the user logged into the system. Both returned values are strings. With the information in hand, you could compare what is returned in order to set other variables based on the results.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13227) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Entering Many Items In a Drop-Down Form Field

One of the controls you can add to a Word form is a drop-down form field. This field is similar to drop-down lists used ...

Discover More

Creating a Drawing Object

Creating simple drawing objects is easy in Excel. All you need to do is use the tools made available on the Drawing toolbar.

Discover More

Stopping Excel from Deleting Macros from a Workbook

When working with very large workbooks, it is possible for Excel to behave erratically. This tip looks at ways you can ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Generating Unique, Sequential Names

Do you need to create a number of words or phrases where you only alter a few letters in each one? If the alterations ...

Discover More

Retrieving Drive Statistics

Need to gather some information about the drives on a system? It can be pretty easy to do using a macro, as shown in this ...

Discover More

Creating and Naming a Worksheet Using a Macro

You can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 1?

2022-12-08 10:43:27

J. Woolley

My Excel Toolbox's NameOf function can return the value of an environment variable. Here are three example cell formulas:
=NameOf("ComputerName")
=NameOf("UserName")
=NameOf("UserProfile")
The following cell formula returns Excel's Application.UserName:
=NameOf("User")
NameOf is described in the following two articles:
https://excelribbon.tips.net/T007850#comment-form-hd (2022-04-06)
https://wellsr.com/vba/2019/excel/vba-udf-to-enhance-excel-cell-and-info-functions/
See https://sites.google.com/view/MyExcelToolbox


2022-12-07 15:59:27

J. Woolley

My Excel Toolbox includes the following dynamic array functions related to Windows environment variables:
=ListEnvironVariables([SkipHeader]) -- returns all environment variables and their values in 2 columns and N rows including header row (optional).
=ListEnvironPath() -- returns items extracted from environment variable PATH in 1 row and N columns.
=ListEnvironPathExt() -- returns items extracted from environment variable PATHEXT in 1 row and N columns.
My Excel Toolbox's SpillArray function simulates a dynamic array in older versions of Excel:
=SpillArray(ListEnviron...())
See https://sites.google.com/view/MyExcelToolbox


2020-09-21 07:22:43

Alex B

If the path is going to be different for each user I would probably start with Environ(“UserProfile”)
Returns something along the lines of C:\Users


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.