VBA & Macro Problems

Fix Excel VBA Shell Error 53 for MD and DIR Commands

Algirdas JasaitisAlgirdas Jasaitis Nov 25, 2026 868 views

Question details

Run a VBA macro that creates an output folder and saves a recursive directory listing to a text file.

Fix Excel VBA Shell Error 53 for MD and DIR Commands
Product
Microsoft Excel VBA
Device & OS
Windows
Scenario
The macro reads a folder path from cell C4 and writes the output of DIR /S to a text file in C:\Temp.
Observed behavior
VBA Shell returns run-time error 53 when it tries to run MD or DIR directly, although the DIR command works in Command Prompt.
Before you start

Enter an existing folder path in cell C4 and make sure you can write to C:\Temp. Open the VBA editor with Alt+F11 and enable macros only if you trust the workbook.

Solution 1Recommended

Run DIR through the Windows command processor

Use the Windows command processor for DIR and redirection, and wait until the listing is complete.

MD and DIR are internal Command Prompt commands, not standalone executable files. VBA must launch the command processor to run them; the /c switch runs the command and then exits.

1
Open the VBA editor

In Excel, press Alt+F11, then select Insert > Module.

2
Add the macro

Paste this code into the module: Sub ExportDirectoryListing() Dim folder As String, outputFolder As String, outputFile As String Dim command As String, exitCode As Long folder = Trim$(ActiveSheet.Range("C4").Value) outputFolder = "C:\Temp" outputFile = outputFolder & "\Dir.txt" If Dir$(outputFolder, vbDirectory) = "" Then MkDir outputFolder command = """" & Environ$("ComSpec") & """ /c dir """ & folder & """ /s > """ & outputFile & """" exitCode = CreateObject("WScript.Shell").Run(command, 0, True) If exitCode <> 0 Then MsgBox "DIR returned exit code " & exitCode End Sub

3
Run and check the listing

Return to Excel, select the sheet containing the path in C4, and run ExportDirectoryListing from Alt+F8. Open C:\Temp\Dir.txt after the macro finishes.

Run DIR through the Windows command processor
Paths and timing: The macro quotes paths so spaces are handled correctly. WScript.Shell.Run uses True to wait for DIR to finish. The output file is overwritten if it already exists.
Free Microsoft Office alternative

Review directory listings in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar interface and Microsoft Office format compatibility, making it easy to move between spreadsheet tools. It does not fix Excel's VBA Shell error, but you can use WPS Spreadsheets to review exported listing data.

Review and organize directory listing data in a spreadsheetOpen and save Microsoft Excel XLSX filesWork in a familiar, easy-to-use interfaceMove existing Office documents into a lightweight suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does DIR work in Command Prompt but fail in VBA Shell?

DIR is built into the Windows command processor. Launch Environ$("ComSpec") with the /c switch to run DIR from VBA.

How do I handle folder paths that contain spaces?

Enclose the source folder and output file paths in double quotes when constructing the command string. The example macro quotes both paths.

How can VBA wait until the text file is ready?

Use WScript.Shell.Run with its third argument set to True, as in the example. VBA then waits for the command to finish before continuing.

Can I create nested folders with one MkDir statement?

No. MkDir creates one folder at a time, so create any missing parent folders first or use another folder-creation method.