try
list of filenames starts at A1, folder starts at B1
Sub MoveFiles()
Dim FldrSrc As FileDialog
Dim FldrDst As FileDialog
Dim srcPath As String 'source folder path
Dim dstPath As String 'destination folder path
Dim fso As Object 'file system object
'Dim srcFolder As Object 'source folder
Dim dstFolder As Object 'destination folder
Dim lastRow As Long 'last row of data
Dim i As Long 'loop counter
Dim srcFile As String 'full path of source file
Dim dstFile As String 'full path of destination file
Dim folderName As String 'name of folder in column B
'Dim folderPath As String 'full path of folder in column B
'get source folder path from user
Set FldrSrc = Application.FileDialog(msoFileDialogFolderPicker)
With FldrSrc
.Title = "Select Source Folder"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button
srcPath = .SelectedItems(1) & "\"
End With
'get destination folder path from user
Set FldrDst = Application.FileDialog(msoFileDialogFolderPicker)
With FldrDst
.Title = "Select Destination Folder"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button
dstPath = .SelectedItems(1) & "\"
End With
'initialize file system object
Set fso = CreateObject("Scripting.FileSystemObject")
'set source folder
'Set srcFolder = fso.GetFolder(srcPath)
'set destination folder
Set dstFolder = fso.GetFolder(dstPath)
'get last row of data
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
'loop through filenames in column A
For i = 1 To lastRow
'get full path of source file
srcFile = srcPath & "\" & ActiveSheet.Cells(i, "A").Value
'check if source file exists
If fso.FileExists(srcFile) Then
'get folder name from column B
folderName = ActiveSheet.Cells(i, "B").Value
'check if folder exists in destination path
If fso.FolderExists(dstPath & "\" & folderName) = False Then
'create folder if it does not exist
dstFolder.SubFolders.Add folderName
End If
'get full path of destination file
dstFile = dstPath & "\" & folderName & "\" & ActiveSheet.Cells(i, "A").Value
'move file to destination path
fso.MoveFile srcFile, dstFile
End If
Next i
'clean up
Set fso = Nothing
'Set srcFolder = Nothing
Set dstFolder = Nothing
End Sub
Bookmarks