Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Self-Aware Macros.
by Allen Wyatt
(last updated October 6, 2021)
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2607) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Self-Aware Macros.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
Do you need your macro to select all the visible worksheets (and just the visible ones)? It's not as easy as it sounds, ...Discover More
If you need to consolidate a single column of data into multiple columns of data, you'll love this macro. It provides a ...Discover More
You can use the Zoom feature of Excel to magnify what Excel shows of your workbook, but it affects the entire screen. ...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 menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.