by Allen Wyatt
(last updated July 30, 2018)
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13227) applies to Microsoft Excel 2007, 2010, and 2013.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Got a workbook cluttered with all sorts of macros? Delete them and you'll make your workbook easier to manage.Discover More
Want to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.Discover More
When you have multiple workbooks open at the same time, Excel allows you to easily switch between those workbooks. How ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.