Tan Duong
Tan Duong

Reputation: 3

Excel macro to move all subfolders to another location

I only know to write a macro to copy a file from Folder A to Folder B

FileCopy "C:\Documents\Folder A\test.xlsx", "C:\Documents\Folder B\test.xlsx"

Could someone help for an Excel macro to MOVE all sub-folders and files under "Folder A" to "Folder B". Thank you

Upvotes: 0

Views: 2012

Answers (1)

skkakkar
skkakkar

Reputation: 2838

Pl Try This: Change folders as per your requirements.

Sub Copy_Folder()
'This example copy all files and subfolders from FromPath to ToPath.
'Note: If ToPath already exist it will overwrite existing files in this folder
'if ToPath not exist it will be made for you.
    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String

    FromPath = "C:\Process_Contract_Notes"  '<< Change
    ToPath = "C:\Process"    '<< Change


    If Right(FromPath, 1) = "\" Then
        FromPath = Left(FromPath, Len(FromPath) - 1)
    End If

    If Right(ToPath, 1) = "\" Then
        ToPath = Left(ToPath, Len(ToPath) - 1)
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

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

    FSO.CopyFolder Source:=FromPath, Destination:=ToPath
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath

End Sub

Upvotes: 1

Related Questions