Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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: DOS From Macros.

DOS from Macros

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


1

Macros are a wonderful way to expand the functionality of a program such as Excel. You may, however, want to expand that functionality even more by executing a DOS batch file from your macro. Excel allows you to do this by using the Shell command. The general syntax for the command is as follows:

dRetVal = Shell("myfile.bat", mode)

Within the quote marks you can place the full path name and file name of the file you want to execute. On some systems you may experience problems if you use a path name with the file specification. (This seems to crop its ugly head if you have complex path names or if the path name includes spaces.) If you have this problem, then simply use the ChDir command just prior to Shell in order to change the directory used by Excel. You can then execute Shell using just a file name.

The mode indicator simply tells Excel how you want the window opened for the file to appear. The mode indicator can be any of the following:

Value Variable Nam Meaning
0 vbHide Window is hidden and has focus.
1 vbNormalFocus Window is the normal size and has focus.
2 vbMinimizedFocus Window is minimized and has focus.
3 vbMaximizedFocus Window is maximized and has focus.
4 vbNormalNoFocus Window is normal size, but doesn't have focus.
6 vbMinimizedNoFocus Window is minimized, but doesn't have focus.

The Shell command returns a value that indicates the program ID of the file you executed, or else a zero. If a zero is returned, then there was an error executing the file.

You should remember that when you use Shell, the target file is executed right away, and it is executed independently of Excel. This means that the next macro command, in your Excel macro, is immediately executed without waiting for the Shell target file to finish. If you need a way around this behavior, you may be interested in this page at Chip Pearson's website:

http://www.cpearson.com/excel/ShellAndWait.aspx

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

Printing Multiple Worksheet Ranges

Need to print more than one portion of your worksheet? If you use named ranges for the different ranges you want to ...

Discover More

Decimal Tab Alignment

If you are familiar with decimal tabs in Word, you may wonder if you can set the same sort of alignment in Excel. The ...

Discover More

Printing and Exiting Word in a Macro

When you print a document, Word remains busy in the background until the printing is done. If you try to end the program ...

Discover More

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!

More ExcelTips (ribbon)

Saving Information in a Text File

The VBA programming language provided with Excel allows you to create and modify text files quite easily. Here's how to ...

Discover More

Updating Automatically when Opening Under Macro Control

If your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...

Discover More

Changing the Default Drive

Do you have a macro that needs to read and write files? If so, then there is a good chance you need to specify the ...

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 1 + 1?

2021-12-25 10:50:12

J. Woolley

My Excel Toolbox includes the following function to create a hyperlink that will run a Shell command:
RunCommandLink(Command_Line, [Friendly_Name], [Screen_Tip])
If Command_Line begins with HIDE:: (ignoring alphabetic case), the remainder will be run in a hidden Shell window (vbHide); otherwise, vbNormalFocus will apply. The function returns Friendly_Name (like the HYPERLINK and SuperLink functions) and adds a hyperlink Screen_Tip (like the SuperLink function). Here are three cell formula examples:
="Click to run "&RunCommandLink("myfile.bat","my batch file")
=RunCommandLink("Notepad.exe ""My Note.txt""",,"Click to run")
=RunCommandLink("Hide::cmd /k dir > ""Dir.txt"" && Notepad ""Dir.txt""")
See https://sites.google.com/view/MyExcelToolbox/


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.