+ Reply to Thread
Results 1 to 7 of 7

Macro help - appending a date to a file name when saving

  1. #1
    Registered User
    Join Date
    03-05-2012
    Location
    Chicago
    MS-Off Ver
    2007
    Posts
    25

    Macro help - appending a date to a file name when saving

    I'm trying to create a macro that saves a file in a specific format (macro-enabled workbook), specific title (Inventory <date in yyyy-mm-dd format>), and in a specific place (the same folder the Inventory template is in rather than defaulting to My Documents). Basically all the user should have to do is click a Save File button I'd add on the worksheet and everything else would be done (to take out the possibility of human error). To easily be able to document inventory history, I'd like to be able to have a folder that just has files like

    Inventory 2012-03-03
    Inventory 2012-03-10
    Inventory 2012-03-17
    etc.

    If it's easier, the word "inventory" could just be taken out and each sheet could just be saved as the date. The yyyy-mm-dd format is preferred, but if it's simpler to do as a different format it's not a big deal. I have a basic knowledge of macros in that I can record them, attach them to things, and do some VERY basic editing of them in VB. How can I set up a macro so that when the user clicks a button the document is automatically saved with the correct file name in the correct folder?
    Last edited by Cyali; 03-15-2012 at 08:37 PM.

  2. #2
    Registered User
    Join Date
    01-26-2012
    Location
    Slovenia; Kranj
    MS-Off Ver
    Office 365
    Posts
    67

    Re: Macro help - appending a date to a file name when saving

    Hi,

    try and play with this ...

    Please Login or Register  to view this content.
    I think date format need to be define by system default (Windows).


    Cheers, Marko

  3. #3
    Registered User
    Join Date
    03-05-2012
    Location
    Chicago
    MS-Off Ver
    2007
    Posts
    25

    Re: Macro help - appending a date to a file name when saving

    I tried putting that in, but Excel didn't like the ActiveWorkbook line. The specific error I got was "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed.

  4. #4
    Registered User
    Join Date
    01-26-2012
    Location
    Slovenia; Kranj
    MS-Off Ver
    Office 365
    Posts
    67

    Re: Macro help - appending a date to a file name when saving

    Hi,

    try with ...

    Please Login or Register  to view this content.
    ... X:\Privat\Scani\ ==> should be some your folder.


    Let me know, Marko

  5. #5
    Registered User
    Join Date
    03-05-2012
    Location
    Chicago
    MS-Off Ver
    2007
    Posts
    25

    Re: Macro help - appending a date to a file name when saving

    That worked, and it saved the document properly. Now how can I make it choose the correct date? If it's going to be an issue using that date format, then it can be a different format. It's just important that the correct date is appended to the file.

  6. #6
    Registered User
    Join Date
    01-26-2012
    Location
    Slovenia; Kranj
    MS-Off Ver
    Office 365
    Posts
    67

    Re: Macro help - appending a date to a file name when saving

    OK,

    if this ...

    Please Login or Register  to view this content.
    ... is working than a problem is in ...

    Please Login or Register  to view this content.
    So if you'll have "fix" folder path, than you can write direct in the code. And date? Is this "today" or depends on some other variables. Could be problem that TodaysDate = Range("C3") or Folder = Range("C2") is not reading properly the value from cell.

    It is hard to give you 99,9% solution if we don't know how does look your file, sheets, ...


    Play with "get data from a specific cell" ... cheers, Marko

  7. #7
    Registered User
    Join Date
    03-05-2012
    Location
    Chicago
    MS-Off Ver
    2007
    Posts
    25

    Re: Macro help - appending a date to a file name when saving

    Is there a way to just append the current date to the file name? The folder will be the same one all the time, and if it happens to change that's something I could just manually change in the code. I'd tried using this:

    Please Login or Register  to view this content.
    and I think I found the issue. When I hit Debug on the error and hovered over "TodaysDate" in the Active.Workbook line, it showed the date as "3/10/2012" and files can't be saved with a /.

    When I changed the cell type that TodaysDate looks at to Text and input the date as "3-10-2012" then it saved properly.

    This brings up one more question - is there a way to do this automatically? I know that ctrl + ; sticks the current date in a cell, but it's formatted as x/x/xx. And changing the cell's formatting to show with dashes instead of slashes doesn't work (as that's how I had it the first time you posted code for me to try). Edit: Nevermind; I can just use an Auto_Run macro to insert the current date in a hidden cell, then use DAY, MONTH, and YEAR functions to separate it.

    If there's no easy way to do it, then this will still work great.

    Also, eventually I plan to make a kind of cover worksheet that has all of the buttons so the user only has to go to one page. In VB, can I reference cells just as in formulas like below?
    Please Login or Register  to view this content.
    Last edited by Cyali; 03-10-2012 at 06:38 PM.

  8. #8
    Registered User
    Join Date
    04-14-2010
    Location
    NZ
    MS-Off Ver
    2007,2010
    Posts
    86

    Re: Macro help - appending a date to a file name when saving

    You could try something like this:

    Please Login or Register  to view this content.

+ 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