Hi,
I was able to get this code worked for files & folders separately.
changed this line FSO.MoveFile to FSO movefolder and this works.
need another help for
1. for the file names, I have to put the file extention also in the excel file like .pdf, .xlsx else it says file doesn't exits. how do I modify this to pick up this.
2. how do I club the code for files and folder together if file is there then move file , if folder is there move folder.
Sub Move_Files_From_One_Folder_To_Another_Folder()
' We define our variables
Dim FSO As Object
Dim FromDir As String
Dim ToDir As String
Dim FExtension As String
Dim FNames As String
Dim Files As String
Dim LR As Long
' We initialize our source and destination directories
LR = Sheets("Macro").Range("B" & Rows.Count).End(xlUp).Row
For RW = 15 To LR
FromDir = Cells(RW, 2).Value & "\"
Files = Cells(RW, 3).Value
ToDir = Cells(RW, 4).Value & "\"
'We define here the types of files we wish to move. If you had .txt or .docx or .doc files you can use *.* as the FExtension
FExtension = Files
'Now assign each file name with extension
FNames = Dir(FromDir & FExtension)
'Check whether there are any files in the folder so that you can exit if there are no files
If Len(FNames) = 0 Then
MsgBox "No files in " & FromDir
Exit Sub
End If
'Most methods return values but FSO returns an object. You cannot simply assign an object to a variable using an equals sign. In Excel VBA you need to use the Set statement
Set FSO = CreateObject("Scripting.FileSystemObject")
'Now we move the file from the source directory to the destination directory
FSO.MoveFile Source:=FromDir & FExtension, Destination:=ToDir
Next RW
End Sub
for folders
Sub Move_Folder_From_One_Folder_To_Another_Folder()
' We define our variables
Dim FSO As Object
Dim FromDir As String
Dim ToDir As String
Dim FExtension As String
Dim FNames As String
Dim Files As String
Dim LR As Long
' We initialize our source and destination directories
LR = Sheets("Macro").Range("B" & Rows.Count).End(xlUp).Row
For RW = 15 To LR
FromDir = Cells(RW, 2).Value & "\"
Files = Cells(RW, 3).Value
ToDir = Cells(RW, 4).Value & "\"
'We define here the types of files we wish to move. If you had .txt or .docx or .doc files you can use *.* as the FExtension
FExtension = Files
'Now assign each file name with extension
FNames = Dir(FromDir & FExtension)
'Most methods return values but FSO returns an object. You cannot simply assign an object to a variable using an equals sign. In Excel VBA you need to use the Set statement
Set FSO = CreateObject("Scripting.FileSystemObject")
'Now we move the file from the source directory to the destination directory
FSO.Movefolder Source:=FromDir & FExtension, Destination:=ToDir
Next RW
End Sub