VBA & Macro Problems

Fix VBA Dir Wildcards in Excel for Mac

Aamir Naveed AkramAamir Naveed Akram Nov 18, 2026 869 views

Question details

Find a Mac-compatible way to list files when a VBA Dir wildcard does not return the expected results.

Fix VBA Dir Wildcards in Excel for Mac
Product
Microsoft Excel for Mac
Device & OS
macOS
Scenario
A macro that finds files using wildcard patterns on Windows needs to work in Excel for Mac.
Observed behavior
The VBA Dir function may fail to find matching files when wildcards are used, particularly in older Office 2016 builds.
Before you start

In Excel, open Excel > About Microsoft Excel and record the version and build. Check the folder path, file extension, and wildcard pattern used by the macro, then confirm that matching files exist in Finder.

Solution 1Recommended

List files by extension with AppleScriptTask

Use a Mac-specific routine when Dir wildcard matching does not work in your Excel build.

This example prompts you to select a folder and returns the names of files with a specified extension. It does not reproduce arbitrary wildcard patterns or return full paths.

The routine requires an Excel for Mac build that supports AppleScriptTask. Use a separate Windows code path if your macro must run on both platforms.

1
Check for available updates

In Excel, choose Help > Check for Updates, if available, and install any offered updates. Office 2016 is no longer supported, so updates may not be available.

2
Create the script folder

In Finder, choose Go > Go to Folder and enter ~/Library/Application Scripts/. Create a folder named com.microsoft.Excel if it does not already exist.

3
Save the AppleScript

Open Script Editor and paste the following script. Choose File > Save, select the Script file format, and save it as ListFiles.scpt in ~/Library/Application Scripts/com.microsoft.Excel/. on listMatchingFiles(fileExtension) set chosenFolder to choose folder with prompt "Select a folder" tell application "Finder" set fileNames to name of every file of chosenFolder whose name extension is fileExtension end tell set oldDelimiters to AppleScript's text item delimiters set AppleScript's text item delimiters to linefeed set resultText to fileNames as text set AppleScript's text item delimiters to oldDelimiters return resultText end listMatchingFiles

4
Call the script from VBA

In Excel, open the Visual Basic Editor, insert a module, and run this example. Change "xlsx" to the extension you need, without a period. Sub ListMacFiles() Dim names As String names = AppleScriptTask("ListFiles.scpt", "listMatchingFiles", "xlsx") If Len(names) = 0 Then MsgBox "No matching files found." Else MsgBox names End If End Sub

5
Test folder access

Select the folder in the prompt and allow any requested automation access. If Finder access is blocked, check System Settings > Privacy & Security > Automation; the settings name and location may vary by macOS version.

Adapt the results to your macro: The returned names are separated by line breaks. If your macro needs to open files, adapt it to obtain and use the selected folder path as well. Canceling the folder prompt or denying access can raise an error, so add error handling for production use.
Free Microsoft Office alternative

Work with Excel files in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work between office suites. It does not fix Excel for Mac's VBA Dir behavior.

Open and edit Microsoft Excel XLSX and XLS filesUse a familiar interface for everyday spreadsheet workMove spreadsheet documents between office suitesUse a lightweight alternative for tasks that do not depend on the affected VBA macro
microsoft office alternative - wps office

Frequently Asked Questions

Which Office 2016 build fixes Dir wildcards on Mac?

A specific minimum build is not established here. Check Excel > About Microsoft Excel for your build, look for available updates under Help > Check for Updates, and test the pattern on your Mac.

Why does my Dir macro work on Windows but not on Mac?

File-handling behavior can differ between Windows and Mac, and wildcard support may depend on the Excel build. Verify the path and matching files in Finder, then use a Mac-specific routine if needed.

Can the AppleScript example match patterns such as report*.xlsx?

No. The example filters by file extension only. To match a filename pattern, extend the AppleScript or filter the returned names in VBA.

Where should I save the AppleScript for Excel?

Save ListFiles.scpt in ~/Library/Application Scripts/com.microsoft.Excel/. In Finder, use Go > Go to Folder to navigate to the Application Scripts folder.