Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Files with Excel VBA

Tags:

Here's what I need to do. I have these two columns in an excel sheet. With file names. First column has the current filename and the second column has the names I want the files to be renamed to. I need to use this as there's no pattern in renaming. For example, the below may be a set of files ...

Current Name >  Rename To --------------------------- Abc.jpg       >   Dinner.jpg  Xyz.jpg       >  Driving.jpg  123.jpg       >  Sunset.jpg 

I know it should be easy to do this in VBA, but not exactly sure how. Any help would be much appreciated.

like image 961
redGREENblue Avatar asked Sep 22 '11 01:09

redGREENblue


People also ask

How do I rename a file in Excel VBA?

Name oldName As newName ' Rename file. oldName = "C:\MYDIR\OLDFILE": newName = "C:\YOURDIR\NEWFILE" Name oldName As newName ' Move and rename file.


1 Answers

I think you could do something like this, using the Name function to rename the files, however, you will probably need to make sure the 2 columns have the complete file path, i.e. "C:\Temp\ABC.jpg"

Dim Source As Range Dim OldFile As String Dim NewFile As String  Set Source = Cells(1, 1).CurrentRegion  For Row = 1 To Source.Rows.Count     OldFile = ActiveSheet.Cells(Row, 1)     NewFile = ActiveSheet.Cells(Row, 2)      ' rename files     Name OldFile As Newfile  Next 
like image 197
PaulStock Avatar answered Sep 20 '22 13:09

PaulStock