logo
search
VBA & Macro Problems

Fix VBA Error When Copying Specific Files to Multiple Folders in Excel

Khadija KhanKhadija Khan Sep 30, 2026 868 views

Question details

The user wants an Excel VBA macro to copy specific files from source paths to destination folders listed in columns, but encounters a "User-defined type not defined" error.

How to Copy Specific Files to Multiple Folders Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Automating file organization by copying files listed in an Excel worksheet (source in Column A, destination in Column B) using FileSystemObject.
Observed behavior
Running the VBA macro triggers a "User-defined type not defined" error due to missing Microsoft Scripting Runtime references for the FileSystemObject.
Before you start

Verify that your Excel worksheet has the exact source file paths in Column A and the complete destination folder paths (including the target file names) in Column B before executing the macro.

Solution 1Recommended

Use Late Binding to Avoid Missing Reference Errors

This is the recommended approach because it eliminates the need to manually enable system references, preventing errors when sharing the workbook with other users.

Late binding allows VBA to determine the object type at runtime rather than at compile time. By declaring the FileSystemObject as a generic Object, you bypass the "User-defined type not defined" error entirely.

1
Access the VBA Editor

Open your Excel workbook and press the Alt + F11 keys simultaneously to launch the Visual Basic for Applications (VBA) editor.

2
Declare the Object Variables

Insert a new module from the Insert menu. Declare your FileSystemObject variable using late binding: type 'Dim fso As Object'.

3
Initialize the FileSystemObject

Create the object instance by adding the code: 'Set fso = CreateObject("Scripting.FileSystemObject")'.

4
Loop and Copy Files

Write a loop to iterate through your source and destination ranges. Inside the loop, execute the copy command using 'fso.CopyFile SourceFile, DestinationFile'.

Use Late Binding to Avoid Missing Reference Errors
Portability Advantage: Late binding ensures your macro runs seamlessly on any computer without requiring users to manually enable the Microsoft Scripting Runtime library.
Advanced Macros in WPS Office

Use WPS Spreadsheet to Run VBA Macros Easily

WPS Spreadsheet seamlessly supports VBA macros and FileSystemObject operations, allowing you to automate complex file management tasks just like Microsoft Excel, all within a lightweight and efficient environment.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your workbook containing the file path lists.
  2. 2. Access the Developer Tools: Navigate to the Developer tab and click on the 'VBA Editor' button to access the macro environment.
  3. 3. Paste and Run Your Code: Paste your late-bound FileSystemObject script into a new module and click 'Run' to copy your files instantly.
Full compatibility with Microsoft Excel VBA scripts and macros.Native support for FileSystemObject (FSO) file operations.Lightweight architecture for faster execution of heavy data processing.Free Microsoft Office alternative with a familiar user interface.
QA img-9

Frequently Asked Questions

Why does my VBA macro throw a 'Path not found' error when copying files?

This error occurs if the destination folder does not exist. The FileSystemObject requires the target directories to be created before executing the CopyFile command. Ensure your macro checks for the folder's existence or creates it dynamically before copying.

Does the destination path need to include the file name?

Yes, when using the FileSystemObject's CopyFile method, your destination string in Column B must include the target folder path as well as the desired file name and extension (e.g., C:\TargetFolder\MyFile.xlsx).

What is the primary difference between Early Binding and Late Binding?

Early Binding requires you to check a specific library reference (like Microsoft Scripting Runtime) in the Tools menu, offering code autocomplete. Late Binding creates the object at runtime (using CreateObject), avoiding missing reference errors when sharing the file with others.