I have a folder which contains one text file
I could use the below code to rename it successfully.
Problem If the initial name of that file is changed, an error raised File not found.
So,I need to rename that file ( what ever name ) on sub folder (Desktop) by using VBA.
In advance, appreciate for any help.
Sub Rename_File()
Dim oldName As String
Dim newName As String
oldName = "C:\Users\Waleed\Desktop\Test.txt"
newName = "C:\Users\Waleed\Desktop\work.txt"
Name oldName As newName
End Sub
CodePudding user response:
Use the following code, assumption is there is only one file in the directory
Sub Rename_File()
Const pathName = "D:\TMP\01" `<= Change accordingly
Dim oldName As String
Dim newName As String
oldName = Dir(pathName & Application.PathSeparator & "*.*")
oldName = pathName & Application.PathSeparator & oldName
newName = pathName & Application.PathSeparator & "work.txt"
Name oldName As newName
End Sub
