VBA & Macro Problems

Excel VBA: Open, Copy, and Close Source Workbooks Dynamically

Maira MehtabMaira Mehtab Nov 18, 2026 869 views

Question details

Use a master workbook to open source workbooks listed in a worksheet, copy their data to specified target sheets, and close them automatically.

Open, Copy, and Close Source Workbooks Dynamically with Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A master workbook contains source file names, folder paths, and destination sheet names in a list.
Observed behavior
The desired macro reads each list row, copies the specified source worksheet data to the matching target sheet, and closes the source workbook without saving it.
Before you start

In the master workbook, check that column C contains file names with extensions, column F contains folder paths, and column D contains existing destination sheet names, starting at row 10. Close the source workbooks before running the macro, and use only trusted files because opening workbooks programmatically can run macros.

Solution 1Recommended

Loop through the master list and copy each source worksheet

Use this VBA macro when each source workbook has the same worksheet name and the master list identifies its destination.

The macro uses ThisWorkbook as the master workbook and stops at the first blank cell in column C. It copies the source sheet's used range to the same cell addresses in the destination sheet, overwriting existing cells in that range; it does not clear older data outside that range.

1
Open the VBA editor

Open the macro-enabled master workbook in Excel, press Alt+F11 where available, and choose Insert > Module in the VBA editor.

2
Add the macro

Paste this code into the module: Sub Get_Source_Data() Dim master As Workbook, source As Workbook Dim namesSheet As Worksheet, target As Worksheet Dim sourceRange As Range Dim folderPath As String, fileName As String Dim r As Long, previousScreenUpdating As Boolean Dim errorMessage As String previousScreenUpdating = Application.ScreenUpdating On Error GoTo Failed Application.ScreenUpdating = False Set master = ThisWorkbook Set namesSheet = master.Worksheets("Names") r = 10 Do While Trim(CStr(namesSheet.Cells(r, "C").Value)) <> "" fileName = Trim(CStr(namesSheet.Cells(r, "C").Value)) folderPath = Trim(CStr(namesSheet.Cells(r, "F").Value)) If folderPath = "" Then Err.Raise vbObjectError + 1, , "Folder path is blank on row " & r If Right(folderPath, 1) <> "\" And Right(folderPath, 1) <> "/" Then folderPath = folderPath & Application.PathSeparator End If Set target = master.Worksheets(CStr(namesSheet.Cells(r, "D").Value)) Set source = Workbooks.Open(folderPath & fileName) Set sourceRange = source.Worksheets("4.1 Operating").UsedRange sourceRange.Copy Destination:=target.Range(sourceRange.Address) source.Close SaveChanges:=False Set source = Nothing r = r + 1 Loop CleanUp: On Error Resume Next If Not source Is Nothing Then source.Close SaveChanges:=False Application.ScreenUpdating = previousScreenUpdating On Error GoTo 0 If errorMessage <> "" Then MsgBox errorMessage, vbExclamation, "Import stopped" Exit Sub Failed: errorMessage = "Row " & r & ": " & Err.Description Resume CleanUp End Sub

3
Match the worksheet names

In the code, change "Names" to the master list sheet name and "4.1 Operating" to the source worksheet name if yours differ. Check that each destination name in column D matches a sheet in the master workbook.

4
Run the import

Save the master file as an .xlsm workbook. In Excel, choose Developer > Macros, select Get_Source_Data, and click Run.

Loop through the master list and copy each source worksheet
If the import stops: The message identifies the list row and error. Check its file path, source sheet, and destination sheet, then correct the issue before running the macro again. The macro closes an open source file and restores the previous ScreenUpdating setting during cleanup.
Free Microsoft Office alternative

Work with Excel spreadsheets 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. It does not fix Excel VBA issues; check macro support before migrating a VBA-dependent workflow.

Open and edit source and master spreadsheet data in WPS SpreadsheetsSupports Microsoft Excel formats including .xlsx and .xlsFamiliar menus make common spreadsheet tasks easy to findFree, lightweight office suite for everyday work
microsoft office alternative - wps office

Frequently Asked Questions

Where should I put the source file paths and destination names?

On the master list sheet, put each file name with its extension in column C, its destination sheet name in column D, and its folder path in column F. The sample starts on row 10 and stops at the first blank in column C.

Can I change which source worksheet is copied?

Yes. In the VBA editor, replace "4.1 Operating" in source.Worksheets("4.1 Operating") with the worksheet name used by your source workbooks.

Does the macro save changes to the source workbooks?

No. It uses source.Close SaveChanges:=False. Save any desired source changes and close those workbooks before running the import.

Why did the macro stop on a particular row?

Check the displayed row for a missing or incorrect folder path, file name, destination sheet, or source sheet. Also check whether Excel can access the source file, then run Developer > Macros > Get_Source_Data again.

Will the macro clear all old data from a target sheet?

No. It overwrites cells covered by the source sheet's used range but leaves cells outside that range unchanged. Clear unwanted old data from the target sheet before running the import if necessary.