VBA & Macro Problems

Find an Open CSV Workbook by Variable Filename in Excel VBA

Steven ParkerSteven Parker Dec 2, 2026 868 views

Question details

Find an open CSV workbook whose name starts with USCOTRN and move its worksheet into a master workbook.

Find an Open CSV Workbook by Variable Filename in Excel VBA
Product
Microsoft Excel
Device & OS
Windows
Scenario
A CSV filename may have a Windows-generated suffix such as (1) or (2), so the macro cannot rely on a fixed filename.
Observed behavior
The macro should identify the open CSV by its filename pattern and move its worksheet after the first sheet in the master workbook.
Before you start

Open the USCOTRN CSV and the master workbook in the same Excel instance. Save the master as an Excel Macro-Enabled Workbook (.xlsm), and enable macros only if you trust the workbook and its code.

Solution 1Recommended

Find the CSV with a wildcard and move its worksheet

Use the Workbooks collection and the Like operator to find a filename with a variable suffix.

The pattern uscotrn*.csv matches filenames that begin with USCOTRN and end in .csv, regardless of the characters between them. The macro converts the name to lowercase before matching, so capitalization does not affect the result.

ThisWorkbook refers to the workbook containing the macro. The code stops if more than one open CSV matches, rather than moving a sheet from an unintended file.

1
Open the VBA editor

In the master workbook, press Alt+F11, then select Insert > Module.

2
Add the macro

Paste this code into the module: Sub MoveUSCOTRNSheet() Dim wbDest As Workbook Dim wbSource As Workbook Dim wb As Workbook Dim matchCount As Long Set wbDest = ThisWorkbook For Each wb In Application.Workbooks If LCase$(wb.Name) Like "uscotrn*.csv" Then matchCount = matchCount + 1 Set wbSource = wb End If Next wb If matchCount = 0 Then MsgBox "No open USCOTRN CSV workbook was found." Exit Sub ElseIf matchCount > 1 Then MsgBox "More than one USCOTRN CSV is open. Close the extra files and try again." Exit Sub End If wbSource.Worksheets(1).Move After:=wbDest.Worksheets(1) End Sub

3
Run the macro

Return to Excel, press Alt+F8, select MoveUSCOTRNSheet, and click Run. Check that the CSV worksheet appears after the first sheet in the master workbook.

4
Save the master workbook

Select File > Save to keep the moved worksheet and the macro in the .xlsm master workbook.

Find the CSV with a wildcard and move its worksheet
Move versus copy: Move transfers the worksheet out of the source workbook. If you need to retain it there, replace .Move with .Copy in the final line of the macro.
Free Microsoft Office alternative

Work with CSV files in WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open CSV and Excel files to continue working with your data when migrating from Microsoft Office. It does not fix Excel-specific VBA issues.

Open and review USCOTRN CSV data in a spreadsheetCompatible with Microsoft Excel formats such as XLSX and XLSFamiliar interface for working with sheets and cellsFree, lightweight office suite for everyday document tasks
microsoft office alternative - wps office

Frequently Asked Questions

Will the macro match USCOTRN (1).csv and USCOTRN (2).csv?

Yes. The asterisk in uscotrn*.csv matches the suffix between USCOTRN and .csv. If both files are open, the macro asks you to close the extra file before continuing.

What if the macro cannot find the CSV?

Open the CSV in the same Excel instance as the master workbook and check that its filename starts with USCOTRN and ends in .csv. Then run the macro again with Alt+F8.

Can I copy the worksheet instead of moving it?

Yes. In the VBA editor, replace .Move with .Copy in the line that places the source worksheet after wbDest.Worksheets(1).

Why does the master workbook need to be an XLSM file?

The .xlsm format can store VBA macros. Use File > Save As and choose Excel Macro-Enabled Workbook before saving the macro in the master workbook.