使用Excel VBA重命名文件 [英] Renaming Files with Excel VBA

查看:845
本文介绍了使用Excel VBA重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要做的。我在Excel表格中有这两列。带文件名第一列具有当前文件名,第二列具有我想要将文件重命名的名称。我需要使用它,因为没有重命名的模式。例如,以下可能是一组文件...

 当前名称>重命名为
---------------------------
Abc.jpg> Dinner.jpg

Xyz.jpg> Driving.jpg

123.jpg>我知道在VBA中应该很容易做到这一点,但不完全确定如何。任何帮助将不胜感激。

解决方案

我想你可以这样做,使用名称函数重命名文件,但是您可能需要确保2列具有完整的文件路径,即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)

'重命名文件
名称旧文件作为新文件

下一个


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.

解决方案

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

这篇关于使用Excel VBA重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆