您好,我正在尝试创建一个Sub,我在一个文件夹中找到一个文件,其名称包含一个特定的短语,如" test_file"并使用不同的名称/扩展名重命名它。我有以下代码似乎不起作用:(我也不知道如何在文件名中搜索特定字符串并根据该字符串执行重命名)
Sub ReName()
Dim myFile As String
Dim myPath As String
Dim mVal As String
mVal = "_12345678_" 'test string'
myPath = "C:\Users\bf91955\Desktop\testfolder\" 'folder path'
myFile = Dir(pathname & "*test_file*") 'real file name is 2222_test_test_file.xlsx'
myFile = myPath & myFile
NewName = "new_test_file & mVal & .xlsx" 'save it as .xlsx and with the new filename including myVal'
Name myFile As myPath & NewName 'rename
End Sub
任何帮助将不胜感激!
答案 0 :(得分:1)
看一下你有几个错误的评论
Sub ReName()
Dim myFile As String
Dim myPath As String
Dim mVal As String
mVal = "_12345678_" 'test string'
myPath = "C:\Users\bf91955\Desktop\testfolder\" 'folder path'
'' You weren't referencing MyPath here - guessing copy and paste error
myFile = Dir(myPath & "*test_file*") 'real file name is 2222_test_test_file.xlsx'
myFile = myPath & myFile
'' This was passing the variable as part of the string
newname = "new_test_file" & mVal & ".xlsx" 'save it as .xlsx and with the new filename including myVal'
Name myFile As myPath & newname 'rename
End Sub