Rename file

A

Always Learning

Good morning guys,

does any one know how I can rename a file using VBA code.

I display a file dialog box allowing the user to pick a .csv file. Because
of the quirks of Excel I need that file to come in as a .Txt file.
I do not want the user to have to rename the file as the existing .csv file
is created by a third party program. My VBA macro then reads the .Csv file
and does it's thing. I need it to be .Txt. How do I rename the file in the
directory before I open it.

Thanks for any help.

Best Regards,

Steve Wilson.
 
R

Rob van Gelder

Name statement:
Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE" ' Define file names.
Name OldName As NewName ' Rename file.

OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName ' Move and rename file.
 
A

Always Learning

Hi Rob,

Fantastic. Works like a charm, Thank you.

Is there a way that I can just copy the file rather than renaming and then
deleting the file so I leave everything as it was?

I appreciate your help.

Best Regards,


Steve Wilson.
 
T

Tom Ogilvy

Just move it with the same name using the code provided.
OldName = "C:\MYDIR\OLDFILE.xls": NewName = "C:\YOURDIR\OLDFILE.xls"
 
A

Always Learning

Hi Tom,
Thanks for taking the time to respond.

Your answer is so simple and obvious I want to know why didn't I think of
that?
I am always trying to think of the big answer when there is an obvious one.

Best Regards,

Steve Wilson.
 
Top