Rename File in VBA

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
74,140
Office Version
  1. 365
Platform
  1. Windows
I have some VBA where I have to take a file named with a ".prn" extension and change the name to a ".txt" extension.

Can anyone give me that code?

Thanks.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about?
Code:
Dim OldName As String, NewName As String

OldName = Application.GetOpenFilename("Text (*.prn),*.prn")
NewName = Left(OldName, Len(OldName) - 3) & "txt"

Name OldName As NewName    ' Rename file.

Best regards,
 
Upvote 0
Barrie,

Thanks! That is exactly what I needed. I tried "REN" and "RENAME", but didn't realize that the name of the function I needed was simply "NAME".
 
Upvote 0
That is how I got to be an MVP in the first place, by learning from other MVPs.

I may be an MVP now, but the learning never stops! 8-)
 
Upvote 0
Is there an equivalently easy way to copy or delete files through VBA? I came to this message board specifically looking for this exact same answer (the rename), but I haven't seen anything this straightforward for copying files. I found a site that explained how to do it by using a function embedded in the kernel32.dll, but there must be an easier way, right?
:twisted:

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,872
Messages
6,181,501
Members
453,047
Latest member
charlie_odd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top