0 votes
in VBA by Beginner (8 points)
ABC
Source PathDestination PathStatus
C:\Users\ABC\Desktop\TEST\75371C:\Users\ABC\Success
C:\Users\ABC\Desktop\TEST\75371\123.docC:\Users\ABC\Check Path/File


I had this code but not working. It suppose to auto create folder if cannot be found on B column

i need some help

 

Sub CopyFolder()
    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 = "Path/Folder Error"
        Else
            FSO.MoveFile Source:=strFromFolder, Destination:=strToFolder, overwritefiles:=False
            Range("C" & x).Value = "Success"
        End If
    Next x
End Sub

 

1 Answer

0 votes
by Expert (892 points)

Try this (Not Tested)

Sub CopyFolder()
    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 Not fso.FolderExists(strToFolder) Then MkDir (strToFolder)
        
        If Len(Dir(strFromFolder, vbDirectory)) = 0 Or Len(Dir(strToFolder, vbDirectory)) = 0 Then
            Range("C" & x).Value = "Path/Folder Error"
        Else
            fso.MoveFile Source:=strFromFolder, Destination:=strToFolder, overwritefiles:=False
            Range("C" & x).Value = "Success"
        End If
    Next x
End Sub

 

by Beginner (8 points)

it dont work.

It has error @

Run-time error 448

Named Argument not found

            fso.MoveFile Source:=strFromFolder, Destination:=strToFolder, overwritefiles:=False

 

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.

...