Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel Move Files From One Folder to Another

· 6 min read · Home Tech

In this article
  1. Why Move Files From Excel?
  2. Method 1: Using the Name Statement (Simple)
  3. Method 2: Using FileSystemObject (More Robust)
  4. Moving Multiple Files Based on Cell Values
  5. Common Problems and How to Fix Them
  6. Alternatives to VBA
  7. Safety Tips and Best Practices
  8. Frequently Asked Questions

You can move files from one folder to another directly within Excel using VBA (Visual Basic for Applications) or by using a simple formula with the FILES function combined with a batch file. The most straightforward method is a short VBA macro that uses the Name statement or the FileSystemObject. This article explains those methods step by step, plus how to move files based on cell values, handle errors, and avoid common pitfalls.

Why Move Files From Excel?

Excel is not a file manager, but many people use it as one because they already have a list of file paths in a spreadsheet. Common scenarios include:

  • You have a list of invoices in column A and want to archive them to a different folder after processing.
  • You receive daily reports with names like report_2025-01-15.xlsx and need to sort them into monthly folders.
  • You maintain a master list of client documents and want to reorganize them without manually dragging each file in Windows Explorer.

VBA is the built-in macro language in Excel. It can access the Windows file system through the Name statement or the FileSystemObject (FSO). Both work, but FSO is more flexible and gives better error messages.

Method 1: Using the Name Statement (Simple)

The Name statement is the simplest way to move a file in VBA. It works like the RENAME command in older DOS, but it can also move a file to a new folder if you change the path.

Here is a basic macro that moves a single file:

Sub MoveOneFile()
    Dim oldPath As String
    Dim newPath As String
    oldPath = "C:\Users\YourName\Documents\OldFolder\file.xlsx"
    newPath = "C:\Users\YourName\Documents\NewFolder\file.xlsx"
    Name oldPath As newPath
End Sub

Notice that the new path must include the same file name (or a new name if you want to rename it while moving). If the destination folder does not exist, you will get an error. Also, the file must not be open in Excel or any other program.

Method 2: Using FileSystemObject (More Robust)

The FileSystemObject is part of the Microsoft Scripting Runtime library. It provides a MoveFile method that is more forgiving and can handle wildcards. To use it, you need to enable the reference in the VBA editor:

  1. Press Alt+F11 to open the VBA editor.
  2. Click Tools → References.
  3. Check Microsoft Scripting Runtime and click OK.

Then you can use this macro:

Sub MoveWithFSO()
    Dim fso As New FileSystemObject
    Dim srcFile As String
    Dim destFolder As String
    srcFile = "C:\Data\Invoices\INV-1001.xlsx"
    destFolder = "C:\Data\Archived\"
    fso.MoveFile srcFile, destFolder
End Sub

Unlike Name, MoveFile does not require you to type the file name again in the destination. It simply moves the file into the folder. If the destination folder does not exist, you will get an error. You can create it first with fso.CreateFolder.

Moving Multiple Files Based on Cell Values

Most real-world tasks involve moving many files listed in a worksheet. For example, column A has full file paths, column B has destination folder paths, and you want to move each one. Here is a macro that loops through rows 2 to 100:

Sub MoveFromList()
    Dim fso As New FileSystemObject
    Dim i As Long
    Dim srcFile As String
    Dim destFolder As String
    Dim lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 2 To lastRow
        srcFile = Cells(i, 1).Value
        destFolder = Cells(i, 2).Value
        If srcFile <> "" And destFolder <> "" Then
            If fso.FileExists(srcFile) Then
                If Not fso.FolderExists(destFolder) Then
                    fso.CreateFolder destFolder
                End If
                fso.MoveFile srcFile, destFolder
            Else
                MsgBox "File not found: " & srcFile
            End If
        End If
    Next i
    MsgBox "Done"
End Sub

This macro checks that the source file exists, creates the destination folder if needed, and shows a message if a file is missing. You can adapt it to your own columns and starting row.

Common Problems and How to Fix Them

Moving files from Excel is generally reliable, but you will likely hit one of these issues:

  • File is open in Excel or another program. Close the file before running the macro. You can add a check using IsFileOpen but it is simpler to just close all Excel workbooks except the one with the macro.
  • Destination folder does not exist. The Name statement fails silently. Use FSO and check FolderExists first.
  • Permission denied. The file may be read-only or you may not have write access to the destination folder. Run Excel as administrator if needed.
  • Path too long or contains special characters. Use the short path (8.3) format or avoid spaces and special characters in folder names.
  • File name already exists in destination. MoveFile will overwrite without warning. You can check fso.FileExists(destFolder & "\" & fso.GetFileName(srcFile)) first.

Alternatives to VBA

If you prefer not to use macros, there are other ways to move files from Excel:

  • Power Query can list files in a folder, but it cannot move them. You can use it to create a list, then copy that list to a text file and use a batch script.
  • Windows batch file with move command. You can generate the batch file content in Excel using a formula like ="move "&A2&" "&B2, then save as a .bat file and run it.
  • File Explorer is the simplest for one-off moves. Use Excel to sort your file list, then manually select and drag in Windows.
  • Third-party automation tools like AutoHotkey or Power Automate can also do this, but they require additional software.

Safety Tips and Best Practices

Moving files is a destructive operation—if you move a file to the wrong folder, it can be hard to find. Follow these guidelines:

  • Always test on a copy of your data first. Create a test folder with dummy files.
  • Use a backup of your file list before running the macro.
  • Add an undo feature if possible. For example, write the old and new paths to a log sheet so you can reverse the move.
  • Be careful with relative paths. Always use absolute paths like C:\Data\... to avoid confusion.
  • Make sure the macro has error handling. Use On Error Resume Next or a proper error handler to avoid stopping mid-loop.

Frequently Asked Questions

Can I move files without opening them?

Yes, VBA can move files that are not open in any program. If the file is open in Excel, Word, or any other application, the move will fail with a permission error. Close the file first or use a programmatic way to close it.

How do I move files based on a condition like date or size?

You can use FileSystemObject to get the file's DateLastModified or Size property. For example, fso.GetFile(srcFile).DateLastModified returns a date. Then you can compare it to a date in a cell and move only if it matches.

What is the difference between Move and Copy in VBA?

MoveFile removes the file from the source folder. CopyFile leaves the original in place. If you want to keep a backup, use CopyFile first, then delete the source with DeleteFile.

Can I move multiple files at once with a wildcard?

Yes, FileSystemObject supports wildcards in the source path. For example, fso.MoveFile "C:\Data\*.xlsx", "C:\Archive\" moves all Excel files from Data to Archive. However, this will fail if any file is open or if a file with the same name already exists in the destination.

How do I run the macro automatically when I open the workbook?

Place the macro code in a module and call it from the Workbook_Open event in the ThisWorkbook module. Or use a button on the sheet that triggers the macro when clicked.