Open CSV Files With Changing Names in Excel VBA
Question details
Adapt an Excel VBA macro to find an employee history CSV file whose name contains a changing number.

- 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.
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.
Find the Report Among Open Excel Workbooks
Use this method when the CSV report is already open in Excel.
In Excel, use File > Open to open the employee history CSV and the destination workbook, overtime workbook.xlsm.
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
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.

Open a Matching CSV From a Folder With VBA
Use this method when the report is not already open and the macro should find it in a known folder.
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.

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.




