Fix VBA Error When Copying Specific Files to Multiple Folders in Excel
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.

- 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.
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.
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.
Open your Excel workbook and press the Alt + F11 keys simultaneously to launch the Visual Basic for Applications (VBA) editor.
Insert a new module from the Insert menu. Declare your FileSystemObject variable using late binding: type 'Dim fso As Object'.
Create the object instance by adding the code: 'Set fso = CreateObject("Scripting.FileSystemObject")'.
Write a loop to iterate through your source and destination ranges. Inside the loop, execute the copy command using 'fso.CopyFile SourceFile, DestinationFile'.

Enable Microsoft Scripting Runtime for Early Binding
If you prefer the benefits of early binding, such as autocomplete (IntelliSense) in the VBA editor, you must manually enable the required reference.
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. Open WPS Spreadsheet: Launch WPS Office and open your workbook containing the file path lists.
- 2. Access the Developer Tools: Navigate to the Developer tab and click on the 'VBA Editor' button to access the macro environment.
- 3. Paste and Run Your Code: Paste your late-bound FileSystemObject script into a new module and click 'Run' to copy your files instantly.

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.




