+ Reply to Thread
Results 1 to 6 of 6

macro to add system date to filename during save

  1. #1
    Registered User
    Join Date
    06-14-2010
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    22

    macro to add system date to filename during save

    I'm looking to see if there is a way to have a macro add a date to part of a filename, while it's saving the file to another directory. Here is the macro as it stands right now(?s are place holders and not actually in the macro):

    Please Login or Register  to view this content.
    The current macro runs fine the first time, but without a change in the filename, the second time it is ran, the user is prompted if they want to replace the file thats already there. My question is if there is code that will enter a date where the ?'s are. The solution doesn't have to be a date, that was just the first thing I thought of that would make the files unique.
    Last edited by Leith Ross; 07-12-2013 at 03:25 PM. Reason: Added Code Tags

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: macro to add system date to filename during save

    I used Windows to rename the file. The example workbook is at http://vbaexpress.com/kb/getarticle.php?kb_id=1041

    Here is the code if you want to explore the methods that I designed.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-14-2010
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: macro to add system date to filename during save

    Thank you very much for the response, but I'm not smart enough to figure that out. I downloaded the example workbook and the buttons produced the message boxes, but did not create additional copies of the workbook (I think that is what it was supposed to do). I'm running excel 2007, so I'm not sure if that is an issue or if I just don't have an understanding of what the expected outcome should've been.

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: macro to add system date to filename during save

    The goal is to generate a unique filename. Take that name and put it in your SaveAs routine.

  5. #5
    Registered User
    Join Date
    01-25-2005
    Posts
    16

    Re: macro to add system date to filename during save

    Quote Originally Posted by paintballlovr View Post
    I'm looking to see if there is a way to have a macro add a date to part of a filename, while it's saving the file to another directory. Here is the macro as it stands right now(?s are place holders and not actually in the macro):

    ChDir "X:\Databases\Cash_Reconciliation\Input"
    ActiveWorkbook.SaveAs Filename:= _
    "X:\Databases\Cash_Reconciliation\Input\lockbox_ach????.xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ChDir "X:\Databases\Mainframe_Files"
    ActiveWorkbook.SaveAs Filename:= _
    "X:\Databases\Mainframe_Files\TNMC_Lockbox????.xlsx", FileFormat:= _
    xlOpenXMLWorkbook, CreateBackup:=False

    The current macro runs fine the first time, but without a change in the filename, the second time it is ran, the user is prompted if they want to replace the file thats already there. My question is if there is code that will enter a date where the ?'s are. The solution doesn't have to be a date, that was just the first thing I thought of that would make the files unique.
    Add this to your code in place of the ????:
    Year(Now()) & Month(Now()) & Day(Now()) & Timer
    note: "Timer" is seconds after midnight. Adding it will handle multiple saves within the same day.
    It will return a string formatted as YYYYMMDDSSSSS.SS ("201371241147.86").

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: macro to add system date to filename during save

    I guess if you want to add a format date string then you could use Format().

    e.g
    Please Login or Register  to view this content.
    or with time to make more unique:
    Please Login or Register  to view this content.

    I changed two routines to which use FSO so you need to add that Reference if you use these.

    In Module2:
    Please Login or Register  to view this content.
    In Module1:
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 07-12-2013 at 02:41 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1