Written by Allen Wyatt (last updated November 6, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
For some macros you may need to determine if there is a way to determine the particular machine on which the macro is operating. For instance, you may have a desktop PC that has a particular directory at D:\OraNT\Plus33, while your notebook PC has the directory at C:\OraNT\Plus33. The macro, of course, needs to detect which machine is in use so that it knows which directory to use for its processing.
There are different ways that this task can be approached. It is possible to create an Excel macro that actually accesses the Windows API and determines the name of the computer on which it is running. Such an approach can get quite involved, however.
An easier way is to just use VBA's DIR command to determine where the desired directory exists. The following will do the trick:
Sub OracleQueries() Dim sTemp As String Dim sGoodPath As String sGoodPath = "D:\OraNT\Plus33\" sTemp = Dir("D:\OraNT\Plus33\nul") If sTemp = "" Then sGoodPath = "C:\OraNT\Plus33\" sTemp = Dir("C:\OraNT\Plus33\nul") End If 'Now have directory information If sTemp <> "" Then 'Process queries using sGoodPath Else MsgBox "Directories not found!" End If End Sub
Notice how the DIR function is used in this example. Normally DIR returns the name of the first file it finds in the requested directory. If the directory is empty, however, DIR returns an empty string—even if the directory actually exists. Since all we want to do is find out if the directory exists (not if there are files in it), it is necessary to append the letters "nul" at the end of the directory path used by DIR. This causes DIR to return an empty string if the directory is not located, or else the characters "nul" if it is (even if the directory is empty).
Toward the end of the macro, sTemp will be empty if neither directory could be located. If one of them was located, then sTemp will not be empty, and sGoodPath will be set to the directory name that can be used in further processing.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11576) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Self-Aware Macros.
Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!
Macros depend on the use of variables to do their work. This tip examines how variables are declared in a macro, using ...
Discover MoreIf you run a macro you used in an older version of Excel on a newer system, it may seem like the macro runs slower. Here ...
Discover MoreYou can create and use all sorts of variables in your macros. This tip examines all the different data types you can specify.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-11-09 09:36:37
J. Woolley
Re. my most recent comment below, apparently the last formula was bifurcated because backslash-nul became newline-ul. Here is a substitute formula:
=LEN(VBAResult("Dir(""D:\OraNT\Plus33"", vbDirectory)")) > 0
2024-11-08 13:19:56
J. Woolley
My Excel Toolbox includes three functions that can return the value of environment variable COMPUTERNAME:
    =NameOf("COMPUTERNAME")
    =VBAResult("Environ(""COMPUTERNAME"")")
    =VLOOKUP("COMPUTERNAME", ListEnvironVariables(), 2)
Re. the Tip's macro, this formula will return TRUE if D:\OraNT\Plus33 is a valid directory:
    =LEN(VBAResult("Dir(""D:\OraNT\Plus33
ul"")")) > 0
Each function has been previously described in my comments attached to the following Tips:
NameOf, see https://tips.net/T006145#comment-form-hd
VBAResult, see https://tips.net/T008030#comment-form-hd
ListEnvironVariables, see https://tips.net/T013227#comment-form-hd
Also, see https://sites.google.com/view/MyExcelToolbox/
2024-11-06 09:04:41
Alan Cannon
A much simpler macro, or even a UDF, containing these statements works well for me:
Dim Compter As String
Compter = Environ$("COMPUTERNAME")
CompName = Compter
I use it as a UDF but it could be embedded in a macro where needed.
2016-05-09 17:08:13
David A. Gray
Getting the machine name is one among many details obtainable through the Windows API that are so useful that I wrote VBA wrappers around them, and put them into a library add-in, along the lines of the optional library add-ins that ship with Microsoft Excel. To date, I haven't invested the effort to port them to C++, but they work well enough without access to the facilities afforded to add-ins implemented in C++.
2016-05-09 09:23:43
Nick London
Jeff C thanks.
Don't you just hate it when MS adds a functionality that you used native whit and ingenuity to acheive. :-)
2016-05-07 20:31:13
Jeff C
environ("computername")
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments