ExcelVBA-替换指定文件夹中所有文件名中的字符串

时间:2018-08-02 00:36:19

标签: excel vba excel-vba

我在一个特定的文件夹中有一组文件(jpg,zip,xlsm,各种不同格式),文件名的末尾都带有字符串“ MONTH”。

我需要遍历文件夹中的每个文件,用输入框的内容替换“ MONTH”,以允许用户输入他们想要的任何月份。

我创建了以下子项,尽管它没有返回任何错误,但文件名没有改变。似乎无法弄清楚我要去哪里,对吗?

Sub FileNameAddMonth()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("C:\User\Desktop\MonthlyReports")
Dim Currentmonth As String
Currentmonth = InputBox("Please input the current month")

For Each file In Folder.Files
        sNewFile = file.Name
        sNewFile = Replace(sNewFile, "MONTH", Currentmonth)
Next
End Sub

1 个答案:

答案 0 :(得分:0)

在for-each循环中添加file.name = sNewFile

相关问题