Help! Excel VBA to copy folder from X to X

harky

Active Member
Joined
Apr 8, 2010
Messages
405
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
How to get this work?

VBA Code:
Sub CopyFolders()
     
    Dim FSO As Object
    Dim OldFolder As String, NewFolder As String
     
    OldFolder = Range("A" & i).Value
    NewFolder = Range("B" & i).Value
     
    Set FSO = CreateObject("Scripting.FileSystemObject")
     
    For i = 7 To Range("A" & Rows.Count).End(xlUp).Row
        If FSO.FolderExists(OldFolder & Cells(i, 1).Value) Then
            FSO.CopyFolder OldFolder & Cells(i, 1).Value, NewFolder & Cells(i, 1).Value, True
        End If
    Next i
     
    Set FSO = Nothing
     
    MsgBox "Copy Complete"
     
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Copy & create folder
C:\Report\01012\1234E:\Report\01012\1234
 
Upvote 0
I had many folder to copy to multiple location.

So best it go by path.
 
Upvote 0
I need some help.. i had some error on the C col.. but no idea what is wrong

VBA Code:
Sub FolderCopy()
    Dim FSO As Object
    Dim strFromFolder As String
    Dim strToFolder As String
    Dim lr As Long

Set FSO = CreateObject("scripting.filesystemobject")


lr = Cells(Rows.Count, "A").End(xlUp).Row
For X = 2 To lr
strFromFolder = Range("A" & X).Value


'Destination directory
strToFolder = Range("B" & X).Value

'get project id
    FSO.CopyFolder _
        Source:=strFromFolder, Destination:=strToFolder, overwritefiles:=True
        
        
        On Error GoTo 0
        If Err.Number <> 0 Then
             Range("C" & X).Value = "Copy error"
        Else
             Range("C" & X).Value = "Success"
        End If
        Next X
End Sub
 
Upvote 0
any one can help me? thx.

i think this part got error
VBA Code:
        On Error GoTo 0
        If Err.Number <> 0 Then
             Range("C" & X).Value = "Copy error"
        Else
             Range("C" & X).Value = "Success"
        End If
        Next X
 
Upvote 0
Hi Found my ans. And this is the code. Thanks

ABC
Source DirectoryDestination DirectoryStatus
C:\Users\ABC\Desktop\TL_DB\Test1E:\Test\Copy Fail or Success

VBA Code:
Sub FolderCopy()
    Dim fso As Object, strFromFolder As String, strToFolder As String, x As Long

    Set fso = CreateObject("Scripting.FileSystemObject")

    For x = 2 To Cells(Rows.Count, "A").End(xlUp).Row
        strFromFolder = Range("A" & x).Value
        strToFolder = Range("B" & x).Value

        If Len(Dir(strFromFolder, vbDirectory)) = 0 Or Len(Dir(strToFolder, vbDirectory)) = 0 Then
            Range("C" & x).Value = "Copy Error"
        Else
            fso.CopyFolder Source:=strFromFolder, Destination:=strToFolder, overwritefiles:=True
            Range("C" & x).Value = "Success"
        End If
    Next x
End Sub
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top