troubleshooting Question

Copy all excel files from one folder to another

Avatar of MPI_IT
MPI_ITFlag for United States of America asked on
Microsoft Excel
16 Comments1 Solution1201 ViewsLast Modified:
I would like to copy all excel files in a folder to another folder using vba code.
I found a vba code which copies excel files from one folder to another, but it does not
copy the files in the subfolders.I will have atleast 2 levels of subfolders .I don't want
to copy the subfolders, only excel files. I have Excel 2007.Any ideas ?
Thanks !
Sub CopyFilesInFolder()

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String

    FromPath = "C:\L1\Temp\R1"  
    ToPath = "C:\L2\Temp\R2"   

    FileExt = "*.xl*"  
  

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If

    If FSO.FolderExists(ToPath) = False Then
        MsgBox ToPath & " doesn't exist"
        Exit Sub
    End If

    FSO.CopyFile Source:=FromPath & FileExt, Destination:=ToPath
    MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Dave

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros