Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

Renaming Files with Excel VBA

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:TempABC.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...