You can import one Excel file into another with VBA in a single procedure: open the source workbook with Workbooks.Open, copy its sheet or range into the target workbook, then close the source without saving. That is the whole pattern. Everything else (picking the file with a dialog, copying only one sheet, pulling in every file in a folder) is a variation on those three lines. This guide walks through building the macro from scratch, including the parts people usually get wrong: ghost Excel processes left behind by unclosed workbooks, the 65,536-row limit on the old file format, and links that break when the imported sheet points back to a source file you then delete.

What You'll Need

  • Microsoft Excel on Windows or Mac with the desktop app. VBA does not run in Excel for the web.
  • A saved target workbook, in .xlsm format if you want the macro to stay in the file.
  • The source file saved somewhere known, ideally with a header row in row 1 and no merged cells above it.
  • The Developer tab visible: File > Options > Customize Ribbon, tick Developer. On Mac it is Excel > Preferences > Ribbon & Toolbar. Menu wording shifts slightly between versions and platforms, so look for the closest match.
  • For macros to be allowed at all, File > Options > Trust Center > Trust Center Settings > Macro Settings must not be set to disable all macros with notification. If it is, you will get a yellow bar instead of a running macro.

Step-by-Step

  1. Open your target workbook and press Alt+F11 (Fn+Alt+F11 on many Mac keyboards) to open the Visual Basic Editor. If you cannot, right-click the workbook name in the Project Explorer and choose Insert > Module.
  2. Paste the basic procedure below into the empty module. Each line is commented so you can see what it does; keep the comments while you test, then delete them later.
    Sub ImportOneFile()
    Dim src As Workbook
    Dim path As String
    path = "C:\Data\sales.xlsx"
    Set src = Workbooks.Open(path, ReadOnly:=True)
    src.Worksheets(1).UsedRange.Copy _
    Destination:=ThisWorkbook.Sheets("Data").Range("A1")
    src.Close SaveChanges:=False
    End Sub
  3. Edit the hard-coded path to match your file, and change "Data" to the name of the sheet in your target workbook where the data should land. If that sheet does not exist yet, create it first, or the macro errors.
  4. Press F5 to run it. Go back to the Excel window: the first sheet of the source file should now appear on your Data sheet starting at A1, with the source workbook closed again.
  5. Decide whether you want formatting copied as well. Copy with a Destination copies values and formats. If you only want values, assign the ranges instead: ThisWorkbook.Sheets("Data").Range("A1").Resize(src.Sheets(1).UsedRange.Rows.Count, src.Sheets(1).UsedRange.Columns.Count).Value = src.Sheets(1).UsedRange.Value. This is also faster on large files.
  6. Add a file picker so the path is not baked in. Insert Dim f As Variant then f = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*"). Check it did not return False (the user pressed Cancel) before opening it. GetOpenFilename only returns a path; it does not open the file.
  7. Turn off screen updating while it runs, and always turn it back on, even on error: Application.ScreenUpdating = False near the top and Application.ScreenUpdating = True at the end. Without the reset, Excel can look frozen after a failure.
  8. To import every workbook in a folder, wrap steps 2 to 4 in a loop over Dir("C:\Data\*.xlsx"). Use a sheet named for the file (src.Name) if the data is judged by month or source, and advance with Dir called again with no arguments.
  9. Save the workbook as .xlsm (File > Save As, choose Excel Macro-Enabled Workbook). A .xlsx target cannot store the macro, and Excel will warn you on close that the code will be lost.
  10. Test with the source file closed, then test with it open already. If it is open, Workbooks.Open returns the existing instance rather than a new one, and closing it at the end will close the user's copy, so guard that step if the same file may be open elsewhere.

Common Mistakes

The most frequent one is closing the source with SaveChanges:=True. That rewrites the original file, and if you copied a formula that references other sheets in the source, it may store references you did not intend. Always close with SaveChanges:=False when importing.

Second is forgetting Set before src = Workbooks.Open(...). Object variables need Set; without it you get error 91, object variable not set. Third is copying into a .xls sheet that already has data below the target cell, which silently overwrites it. Clear the destination range first if the row count can grow. Fourth is importing a sheet with merged cells in the header: UsedRange includes them, and later sorting or filtering by column breaks. Fifth, and easy to miss, is importing a file whose sheet uses a date format the target does not recognise; values arrive as text and stop calculating. Check one date column after the first test run.

Tips

Copy only the range you need rather than the whole sheet. If the source has a notes row at the bottom or blank space to the right, UsedRange drags it along. Reading src.Sheets(1).Range("A1").CurrentRegion usually gives a tighter, contiguous block.

Import into a staging sheet, then move or append rows into your main table with a formula or a second VBA step. That keeps the import repeatable: each run overwrites the staging sheet cleanly instead of stacking duplicates.

Record what was imported. Write src.Name and Now() into two cells on the staging sheet. When something looks wrong a month later, you can see which file supplied the bad rows and when it ran.

Keep the macro path in a named cell or a constant at the top of the module rather than in the middle of the code. Renaming a folder then becomes a one-line edit instead of a hunt.

For a source you must not alter at all, open it ReadOnly (as in the code above) and in a separate window. That rules out accidental saves from the import.

Troubleshooting

If you get error 1004 on Workbooks.Open, the path is usually wrong, the file is open in another program holding a lock, or the extension does not match the actual format. Check the path with a simple message box before opening.

If Excel is slow or the window flickers, screen updating was not reset. Add an error handler that restores it: On Error GoTo done before the work, the label done: at the end, and Application.ScreenUpdating = True under it.

If a stray EXCEL.EXE process stays in Task Manager after a failure, a workbook was opened and never closed. Every successful Workbooks.Open needs a matching Close, and a failed run leaves the source open. Fix the handler so it closes src even when the copy step errors.

If only part of the data arrives, the source sheet's data extends beyond the range you copied, or the destination sheet name is wrong and the paste landed somewhere you have not looked. Scroll to A1 on every sheet to find it.

If the macro is not listed when you try to run it, it may be in a different module, saved in Personal.xlsb, or blocked by macro settings. Check which workbook is active in the Project Explorer.

Frequently Asked Questions

Can VBA import a CSV in the same way?

No. Opening a CSV with Workbooks.Open makes Excel parse it using the local list separator, and import is one sheet only. Use Copy into a single sheet, or better, use Power Query for CSVs where you need to control the delimiter and date format.

Can I import without opening the source workbook?

Only for simple cases. You can read a closed file with a query or with ADO, but that is a different technique. The Open, Copy, Close pattern is the reliable one and is what most people mean by importing a file.

Will the imported sheet keep its links to the source file?

No. Copying values and formats does not carry formulas that point outside the copied range. Formulas inside the range whose references stay within the imported cells work normally, but anything pointing at another file in the source breaks once that file is closed or moved.

Why does the macro fail on a sheet with more than 65,536 rows?

You are pasting into an .xls target. That format stops at 65,536 rows and 256 columns. Save the target as .xlsm or .xlsx first, then rerun.

Can I import from a workbook that has several sheets?

Yes. Loop over src.Worksheets and copy each one to its own new sheet in the target, naming it src.Name. Watch for duplicate names if you import the same file twice.