rename and move a file in VBa

I need to rename and then move a file in VBA.  How do i do that?
vbnetcoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Easy: Name OldFile As NewFile.

Where OldFile and NewFile are the complete file name including the old and new path.

Obviously, it's a good idea to test whether the old file exist, that the new path exists and that there is no file of the same name.
vbnetcoderAuthor Commented:
you have some code?
ste5anSenior DeveloperCommented:
Name OldFile As NewFile is the code you need. E.g.

Name "C:\Temp\Book1.xlsx" As "C:\Temp\Test\Book123.xlsx"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

vbnetcoderAuthor Commented:
How do i check if it exists first?
Rey Obrero (Capricorn1)Commented:
to check if a file exists,

if dir(strFullPathAndName, vbdirectory)<> "" then
     'file exists
   else
      'file doesn't exists
end if
vbnetcoderAuthor Commented:
This code always always displays the file exists msgbox

Dim strFilePath1
strFilePath1 = ""
If Dir(strFilePath1, vbDirectory) <> "" Then
     MsgBox "file exsists"
     
   Else
      MsgBox "file does not exsist"
End If

Open in new window

Rey Obrero (Capricorn1)Commented:
that is not the way it works, you have to pass a fullpath and fileName

Dim strFilePath1
strFilePath1 = "c:\folder\myfile.csv"   ' if myfile.csv
If Dir(strFilePath1, vbDirectory) <> "" Then
     MsgBox "file exsists"
     
   Else
      MsgBox "file does not exsist"
End If
vbnetcoderAuthor Commented:
ty
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.