Macro to move specific files from one folder to another folder

p9j123

Active Member
Joined
Apr 15, 2014
Messages
288
Office Version
  1. 2013
Platform
  1. Windows
I have a list of files on column A along with their path, I would need help in creating a macro that will move this file to the path in column b.

C:\Origin\test1.xlsC:\Origin\First\
C:\Origin\test2.xlsC:\Origin\Second\
C:\Origin\test3.xlsC:\Origin\Third\
C:\Origin\test4.xlsC:\Origin\Fourth\
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this
Your data should start in cell A2
VBA Code:
Sub Rename_and_move_files()
  Dim Path1 As String, Path2 As String, sName As String
  Dim i As Long

  For i = 2 To Range("A" & Rows.Count).End(3).Row
    Path1 = Range("A" & i).Value
    Path2 = Range("B" & i).Value
    If Right(Path2, 1) <> "\" Then Path2 = Path2 & "\"
    sName = Mid(Path1, InStrRev(Path1, "\") + 1)
    If Dir(Path1) <> "" Then
      If Dir(Path2, vbDirectory) <> "" Then
        Name Path1 As Path2 & sName
      End If
    End If
  Next
End Sub
 
Upvote 0
... an alternative could be
VBA Code:
Public Sub MoveFilesByList()
    Dim fso As Object, f As Object
    Dim r As Long

    r = 1   'row to start with
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Do While Not IsEmpty(Cells(r, 1))
        If fso.FileExists(Cells(r, 1)) Then
            Set f = fso.GetFile(Cells(r, 1))
                If fso.FolderExists(Cells(r, 2)) Then
                    f.Move Cells(r, 2) & "\"
                Else
                    MsgBox "Folder " & Cells(r, 2) & " doesn't exist"
                End If
        Else
            MsgBox "File " & Cells(r, 1) & " doesn't exist"
        End If
        r = r + 1
    Loop
End Sub
 
Upvote 0
Solution
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,533
Members
448,969
Latest member
mirek8991

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