To copy a file from File Explorer using VBA, use the FileSystemObject.CopyFile method: create a Scripting.FileSystemObject, then call CopyFile SourcePath, DestinationPath. This works in Excel, Word, Access, or any VBA host and copies the file without opening Explorer's UI. You need the source path (the full folder and filename) and a destination folder that already exists. The steps below show how to wire it up, handle overwrites, and avoid the usual path errors.
What You'll Need
- Any Microsoft Office app with the VBA editor (Alt+F11). Excel is used in the examples.
- The full source path, including the filename and extension, e.g.
C:\Reports\data.xlsx. - A destination folder that already exists.
CopyFilecopies a file into a folder; it does not create folders. - Permission to read the source and write to the destination. Network shares and OneDrive folders can behave differently from local drives.
Step-by-Step
- Open the VBA editor with Alt+F11, then insert a module via Insert > Module. Labels in the editor can differ slightly by Office version and region, but the Insert menu is consistent.
- Add a reference to the Scripting Runtime if you want early binding: Tools > References, tick Microsoft Scripting Runtime, then click OK. This gives you IntelliSense for
FileSystemObject. If you skip this, use late binding (next step) and the code still runs. - Write the copy routine. Early-bound version:
Sub CopyOneFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
fso.CopyFile Source:="C:\Reports\data.xlsx", _
Destination:="C:\Archive\data.xlsx", _
OverWriteFiles:=True
Set fso = Nothing
End Sub
Late-bound version (no reference needed):
Sub CopyOneFileLateBound()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\Reports\data.xlsx", "C:\Archive\data.xlsx", True
Set fso = Nothing
End Sub
- Set
OverWriteFilesdeliberately.Truereplaces an existing file at the destination;Falseraises a run-time error if the file is already there. For a one-off copy,Falseis safer because it stops you from silently overwriting something. - Run the macro with F5. If the destination folder does not exist, you get error 76 (Path not found). Create the folder first, or add a check with
fso.FolderExistsbefore copying. - To copy every file in a folder, pass a wildcard as the source and a folder as the destination:
fso.CopyFile "C:\Reports\*.xlsx", "C:\Archive\". The trailing backslash matters; without it, VBA may treat the destination as a filename. - To let the user pick a file from Explorer instead of hard-coding a path, add a file dialog. In Excel:
Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx"). In other hosts, use the FileDialog object or a simpleInputBoxfor the path. The dialog returns a string you can feed straight intoCopyFile. - Wrap the copy in error handling so a missing file or locked destination does not crash the macro:
On Error GoTo Failed
fso.CopyFile sourcePath, destPath, False
On Error GoTo 0
Exit Sub
Failed:
MsgBox "Copy failed: " & Err.Description, vbExclamation
- Test with a small dummy file in a local folder before pointing the macro at production data. Confirm the file appears at the destination and that the original is unchanged.
Common Mistakes
- Passing only a folder as the source.
CopyFileexpects a file path or a wildcard. A bare folder path raises an error. - Forgetting the destination folder must exist. The method copies files, not directories. Use
fso.CreateFolderfirst if needed. - Omitting the trailing backslash on a folder destination.
C:\Archivecan be read as a filename;C:\Archive\is unambiguous. - Assuming
OverWriteFilesdefaults to False. It does not; pass it explicitly every time. - Copying a file that is open in another program. Windows may lock it and the copy fails with a permission error. Close the file first.
- Using mapped drive letters that are not available in the VBA host's session. Prefer UNC paths like
\\server\share\file.xlsxfor network locations. - Hard-coding user-specific paths such as
C:\Users\Name\Documents. UseEnviron("USERPROFILE")orfso.GetSpecialFolderinstead.
Tips
- Use
fso.FileExistsandfso.FolderExiststo validate both ends before copying, and show a clear message if either is missing. - For recurring copies, store source and destination in named cells or a settings sheet rather than editing code each time.
- If you only need to move a file,
fso.MoveFileworks the same way and removes the original. - Keep a timestamp in the destination filename (e.g.
data_20250101.xlsx) to avoid overwrites entirely. - Test macros on a copy of your data, and save the workbook as .xlsm so the code persists.
Troubleshooting
If the macro stops with Path not found (error 76), check that both the source file and the destination folder exist and that the path has no typos or missing backslashes. If you get Permission denied (error 70), the file may be open, read-only, or on a share where your account lacks write access; close the file and confirm access in Explorer. If File not found (error 53) appears, the source path is wrong or the file was moved. For late-binding errors mentioning the class not registered, the Scripting Runtime may be blocked by policy; early binding with the reference ticked often gives a clearer message. If the copy succeeds but the file is zero bytes, the source was likely still being written by another process; wait and retry. On OneDrive or SharePoint-synced folders, files may be online-only placeholders; open them once to download before copying, or use the synced local path. If nothing happens at all, confirm macros are enabled and that you ran the correct procedure.
Frequently Asked Questions
Can VBA copy a file without the Scripting Runtime?
Yes. You can call the Windows API CopyFile via Declare, but it is more complex and needs 64-bit/32-bit handling. FileSystemObject is the simpler, supported route in Office VBA.
Does CopyFile create the destination folder?
No. It copies files only. Create the folder first with fso.CreateFolder or ensure it exists.
How do I copy a file and rename it at the same time?
Pass the new name in the destination argument, e.g. fso.CopyFile "C:\a\data.xlsx", "C:\b\data_backup.xlsx".
Why does my macro work on my PC but fail on a colleague's?
Paths are usually hard-coded or drive letters differ. Use UNC paths and Environ("USERPROFILE") so the code resolves per user.
Can I copy files from a network share?
Yes, if your account has read access. Use the UNC path rather than a mapped drive, which may not exist in the VBA session.
Does CopyFile work with wildcards?
Yes. *.xlsx or data*.* in the source copies all matching files into the destination folder.