VBA & Macro Problems

Open CSV Files With Changing Names in Excel VBA

Steve KSteve K Nov 18, 2026 870 views

Question details

Adapt an Excel VBA macro to find an employee history CSV file whose name contains a changing number.

Open CSV Files With Changing Names in Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A macro needs to copy report data into a destination workbook when the CSV name changes from one export to another.
Observed behavior
The macro should identify the report by its consistent name rather than requiring a separate macro for each numbered filename.
Before you start

Open the destination macro-enabled workbook and confirm the destination sheet name and folder containing the CSV report. Enable macros only if you trust the workbook and its code.

Solution 1Recommended

Find the Report Among Open Excel Workbooks

Use this method when the CSV report is already open in Excel.

1
Open both files

In Excel, use File > Open to open the employee history CSV and the destination workbook, overtime workbook.xlsm.

2
Add the macro

Open the VBA editor with Alt+F11, select the destination workbook, and choose Insert > Module. Paste this code: Sub ObtainEmployeeData() Dim wbk As Workbook Dim dest As Worksheet Set dest = Workbooks("overtime workbook.xlsm").Worksheets("Sheet1") For Each wbk In Workbooks If InStr(1, wbk.Name, "employeehistoryreport", vbTextCompare) > 0 Then wbk.Worksheets(1).Range("A2:AA600").Copy Destination:=dest.Range("A2") Exit Sub End If Next wbk MsgBox "No open employee history report was found." End Sub

3
Set the destination and run

Replace Sheet1 with the actual destination sheet name and adjust A2:AA600 or the destination cell if needed. In Excel, open the Macros dialog with Alt+F8, select ObtainEmployeeData, and click Run.

Find the Report Among Open Excel Workbooks
Multiple matching reports: The macro copies from the first open workbook whose name contains employeehistoryreport. Close other matching reports before running it if you need a specific file.
Free Microsoft Office alternative

Work With CSV Reports in WPS Spreadsheet

WPS Office is a free, lightweight, easy-to-use alternative for opening and editing spreadsheet reports. Its familiar interface supports a smooth move from Microsoft Office, though it does not fix Excel VBA issues and macro support and compatibility may vary.

Open CSV reports and work with their data in WPS Spreadsheet.Compatible with common Microsoft Excel formats such as XLSX and XLS.Use a familiar spreadsheet interface when moving from Microsoft Office.Work with a free, lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Can the macro find a report if its number changes?

Yes. For an open report, InStr can match the consistent employeehistoryreport portion of the workbook name without specifying the number.

Does the open-workbook macro open the CSV for me?

No. Open the CSV through File > Open first, or use the folder-search macro with Dir and Workbooks.Open.

What happens if more than one report matches?

The open-workbook loop uses the first matching open workbook, and Dir returns the first matching filename. Close or remove extra matches, or add more specific selection logic.

How do I change where the copied data goes?

In the VBA code, replace Sheet1 with your destination worksheet name and change dest.Range("A2") to the desired starting cell.