Hi All,

I want to come up a macro which will rename any of the files .i.e. file with different extensions present in a directory on the basis of the list present in a excel sheet. For e.g. I have old filename in Column A of sheet1 and in Column B I have new file names. The macro should rename the files with the names present in Column B.

Here is the code which I have so far.

Code to list the files present in a directory. This
Sub ListDirectoryFiles ()
Dim myextn As String
myextn = InputBox("Enter the file Extension", "Enter Extension")
If myextn = "" Then Exit Sub
Const ListDir = "C:\Datafiles\"
If Not Dir(ListDir & "*." & myextn) = "" Then
Flist = Dir(ListDir & "*." & myextn)
R = 1
Do Until Flist = ""
Cells(R, 1).Value = Flist
R = R + 1
Flist = Dir
Loop
End If
End Sub
Code to rename files :
Sub RenameFiles()
Dim OldFileName As String
Dim NewFileName As String

For i = 1 To Range("A1").End(xlDown).Row
OldFileName = Cells(R, 1).Value
NewFileName = Cells(R, 2).Value
If Not Dir(OldFileName) = "" Then Name OldFileName As NewFileName
Next
End Sub
The Code to list directory files works fine but the code to rename files doesn't work.

Thanks a lot for your help in advance.