嗨,我正在尝试创建一个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
任何帮助,将不胜感激!
看看你有几个错误的评论
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句