VB Help: Rename existing file to include last modified date in filename?

jeffcoleky

Active Member
Joined
May 24, 2011
Messages
274
I have two scenarios where I need to rename an existing filename to append that file's last modified date/time to the end of the file. The most recently saved file needs to retain the original filename without a date.
Directory: C:\temp
Existing Filename: FileName.csv
File's last Mod Date/Time: 3/2/2017 5:00a​

Using this information above, if within a macro I try to save FileName.csv and it already exists, it should rename the existing FileName.csv to "FileName 03022017 0500.csv" and then save current file as FileName.csv.

I suppose you could "copy" the existing FileName.csv to "FileName date time.csv" instead of saving as, to avoid sharing issues?

Here are two of my macros I need help modifying that will rename the existing FileName.csv before saving:
<need to="" save="" existing="" filename.csv="" instead="" of="" killing="" it.

<need to="" save="" existing="" filename.csv="" instead="" of="" killing="" it.
Code:
Sub Save_Existing_CSV_WITH_MODDATE() 'Rename Filename.csv before doing a SaveAs Filename.csv  
    If exist Then Kill "C:\temp\FileName.csv" 'Don't want it killed...
    Range("A1").FormulaR1C1 = "text"
    Set myrng = ActiveWindow.RangeSelection
    Set newbook = Workbooks.Add
    With newbook
    .Title = "FileName"
    .SaveAs FileName:="C:\temp\FileName.csv", FileFormat:=xlCSV
    End With
   
    Workbooks("FileName.csv").Save
    Workbooks("FileName.csv").Close


End Sub

Code:
Sub Save_Existing_CSV_WITH_MODDATE() 'Rename currently open Filename.csv before doing a save on Filename.csv
 
    Workbooks.Open ("C:\temp\FileName.csv") 'Need to save existing filename.csv before modifying original.
    
    Windows("FileName").Activate
    Range("A1").FormulaR1C1 = "text"    
    Workbooks("FileName.csv").Save
    Workbooks("FileName.csv").Close

End Sub</need>

Any ideas?</need>
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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