VBA & Macro Problems

How to Make Excel VBA Find a CSV File When Its Name Changes

Emma BrownEmma Brown Nov 18, 2026 871 views

Question details

Find or reference a CSV file in an Excel VBA macro without hard-coding a filename that changes with the date and time.

Make Excel VBA Find a CSV File When Its Name Changes
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro needs to work with a CSV file whose name includes a changing timestamp.
Observed behavior
The macro should identify the intended CSV file through a stable filename pattern, user selection, or an already-open workbook.
Before you start

Identify the CSV folder and the part of the filename that stays the same. If several files may match, decide whether the user should select the intended file rather than relying on the first match.

Solution 1Recommended

Find a Matching CSV File with the VBA Dir Function

Use a wildcard when the file is in a known folder and its name has a consistent suffix.

1
Open the VBA editor

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

2
Add the search macro

Paste this example into the module: Sub OpenMatchingCSV() Dim fileName As String Dim folderPath As String folderPath = "C:\Data\" fileName = Dir(folderPath & "*_detail_inbound.csv") If fileName = "" Then MsgBox "No matching CSV file was found.", vbExclamation Exit Sub End If Workbooks.Open folderPath & fileName End Sub

3
Update and run the macro

Replace C:\Data\ with your CSV folder path, including the trailing separator, and change the wildcard pattern to match your filename. Run OpenMatchingCSV from the VBA editor or Excel's macro list.

Find a Matching CSV File with the VBA Dir Function
Multiple matches: Dir returns the first matching name; it does not guarantee the newest file. Use the file picker if you need to choose among matches.
Free Microsoft Office alternative

Open and Review CSV Files in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open CSV and Excel files in WPS Spreadsheets to make migration easier; Excel VBA macro compatibility depends on your WPS edition and configuration.

Open and review CSV data in WPS SpreadsheetsCompatible with Microsoft Excel formats such as XLSX and XLSFamiliar spreadsheet interface for an easier transitionFree and lightweight office tools
microsoft office alternative - wps office

Frequently Asked Questions

How do I match a changing date in a CSV filename?

Use an asterisk in a Dir pattern, such as Dir(folderPath & "*_detail_inbound.csv"), to match any prefix before the fixed suffix.

Does Dir automatically find the newest CSV file?

No. Dir returns the first matching filename, not necessarily the newest. Use a file picker or add logic to compare file timestamps if the newest file is required.

What happens if the user cancels the file picker?

Application.GetOpenFilename returns False when canceled. Check for False and exit the macro before calling Workbooks.Open.

Can I use a CSV that is already open?

Yes. Activate the CSV workbook in Excel, then use ActiveWorkbook or store ActiveSheet in a Worksheet variable before switching to another workbook.