Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 15
Default How do I copy a file and attach a date to the name of the file

Hi there,
I'd like to routinely copy a xls file to a location (for archive) thus want
to attach/append the date to the name of the file.
Thx.
cg
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default How do I copy a file and attach a date to the name of the file

Try some code like the following. It will save a copy of the file, not the
file itself, with the date and time following the file name (but before the
".xls"). Change the constant value C_DATETIME_FORMAT to the date and/or
time format you want to use in the file name. Note that you can't use a ":"
character to delimit the time.

The code will save "C:\Test\Book1.xls" as "C:\Test\date_and_time_Book1.xls".

Sub SaveCopyAsArchive()
Dim Path As String
Dim FName As String
Dim Extension As String
Dim LastSlashPos As Integer
Dim PeriodPos As Integer
Dim FullFileName As String

'<<<<<<< CHANGE BELOW TO DESIRED DATE/TIME FORMAT
Const C_DATETIME_FORMAT = "_hh\;mm\;ss_dd_mmm_yyyy"

If ActiveWorkbook.Path = vbNullString Then
MsgBox "You must save the file first."
Exit Sub
End If

FullFileName = ThisWorkbook.FullName

LastSlashPos = InStrRev(FullFileName, "\", -1, vbBinaryCompare)
PeriodPos = InStrRev(FullFileName, ".", -1, vbBinaryCompare)

Path = Left(FullFileName, LastSlashPos)
Extension = Mid(FullFileName, PeriodPos)
FName = Mid(FullFileName, LastSlashPos + 1, PeriodPos - LastSlashPos -
1)

FullFileName = Path & FName & Format(Now, C_DATETIME_FORMAT) & Extension
ThisWorkbook.SaveCopyAs Filename:=FullFileName
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)




"hccatmo" wrote in message
...
Hi there,
I'd like to routinely copy a xls file to a location (for archive) thus
want
to attach/append the date to the name of the file.
Thx.
cg



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
attach a file to a cell in a spreadsheet chance Excel Discussion (Misc queries) 1 January 1st 07 04:17 AM
Saving worksheet in new file with date AND cell value as file name michaelberrier Excel Discussion (Misc queries) 4 May 26th 06 08:05 PM
can we attach an image file to a excel cell ,like note pad comment PR New Users to Excel 1 January 20th 06 05:08 PM
copy excell file to a CSV file and than to Notepad need to know bob Excel Discussion (Misc queries) 0 August 23rd 05 07:27 PM
copy/paste from one file to another without file name reference slvrblt Excel Discussion (Misc queries) 4 February 6th 05 08:57 PM


All times are GMT +1. The time now is 07:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"