部分重命名多个Excel文件

时间:2017-05-30 22:11:24

标签: vba excel-vba excel

我需要有关部分重命名excel表的帮助。 我有大约40个这些需要每月部分重命名。

例如:

     2017_06 Jun QFR Planning File,

     2017_06 Jun QFR Analytics File,

     2017_06 Jun QFR Customer Service File

需要重命名为

     2017_07 Jul QCR Planning File

     2017_07 Jul QCR Analytics File  

     2017_07 Jul QCR Customer Service File . 

请帮我自动化。我尝试在线查看,但每个VBA代码似乎都完全改变了名称。我希望文件具有各自的文件名,并成功部分重命名。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

编辑1:使用Name As声明

您可以使用Name As这样的方法:

'/* Syntax: Name [oldfilename] As [newfilename] */
'/* where oldfilename and newfilename are full file paths */
Name "C:\Test\2017_06 Jun QFR Planning.xlsx" As "C:\Test\2017_06 Jun QCR Planning.xlsx"

如果你有很多文件,那么你可以加入一个循环。

Dim xlFile As Variant
Dim nFile As String

xlFile = Dir("C:\Test\*.xls*") '/* Folder that contains the files */

Do While xlFile <> "" '/* check if anything is returned */
    If InStr(xlFile, "QFR") <> 0 Then '/* check if there is 'QFR' in the filename
        nFile = VBA.Replace(xlFile, "QFR", "QCR") '/* replace if there is */
        Name xlFile As nFile '/* rename the file */
    End If
    xlFile = Dir '/* Test for more */
Loop