Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Self-Aware Macros.

Self-Aware Macros

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


3

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:

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 (11576) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Self-Aware Macros.

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

Writing a Macro from Scratch

Creating macros can help extend what you can do in Word. If you work with macros, you know that creating macros from ...

Discover More

Entered Values are Divided by 100

Enter a value into a cell and "poof!" it is automatically divided by 100. What's going on, and how can you fix it so that ...

Discover More

Automatically Adding Tabs in Footnotes

When you add a footnote to a document, Word's normal formatting adds a space after the footnote number and before the ...

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)

Understanding the Personal Workbook Filename

The personal macro workbook is a great place to save your macros that you want to use with any workbook. The personal ...

Discover More

Macro for Month Name

Need to know how to generate a full month name based on a date? It's easy to do, as discussed in this tip.

Discover More

Unprotecting Groups of Worksheets

Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can ...

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 one less than 9?

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")


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.