0 votes
in VBA by Beginner (8 points)

I had a code that will copy whole folder from A location to B location, but there is a error on the status. I need some help.

 

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

 

by Expert (892 points)
Hello. I have tested the code and it worked fine for me. Can you put snapshot at the rows that have the error? Make sure of the folder paths in columns A & B.
by Beginner (8 points)

Hi. If the path is incorrect. @ C column, it will state 'Copy error'

but when i run, it show a pop-up instead. 

by Beginner (8 points)
i had hundren of file to copy.. so if there is copy error or success, it will status in Column C

1 Answer

+1 vote
by Expert (892 points)
selected by
 
Best answer

Try this 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

The question posted here too

https://www.mrexcel.com/board/threads/help-excel-vba-to-copy-folder-from-x-to-x.1127885/

by Beginner (8 points)
+1

oh great. I just tested. It work great now. thanks smiley

by Expert (892 points)
+1
You're welcome. Glad I can offer some help.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...