Results 1 to 4 of 4

Thread: Date in Filename

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    3

    Date in Filename

    I am currently writing a macro for use in Excel, which selects a range of cells, pastes them into a new sheet and saves them. What I would like it to do, is have the macro include the date in the filename... is this possible, and if so, how?

  2. #2
    Member
    Join Date
    Mar 2002
    Location
    Grimsby (up the Mariners!)
    Posts
    45
    You can use the
    VB Code:
    1. Format()
    function with
    VB Code:
    1. Date
    to get the current date into the filename.

    VB Code:
    1. strFileName = "MyFilePrefix_" & Format(Date,"yyyy-mm-dd")

    will return MyFilePrefix_2004-10-13.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2004
    Posts
    3
    I've got this so far, but it either returns an error, or saves the file as FALSE.xls

    Code:
    Sub ExportSheet()
        Cells.Select
        Selection.Copy
        Workbooks.Add
        ActiveSheet.Paste
        Range("A1").Select
        Application.CutCopyMode = False
        ActiveWorkbook.SaveAs strFileName = "MyFilePrefix_" & Format(Date, "yyyy-mm-dd")
    End Sub
    Any advice?

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    You've tried to do 2 operations on the same line, change your code to read :-

    VB Code:
    1. Sub ExportSheet()
    2.     Cells.Select
    3.     Selection.Copy
    4.     Workbooks.Add
    5.     ActiveSheet.Paste
    6.     Range("A1").Select
    7.     Application.CutCopyMode = False
    8.     strFileName = "MyFilePrefix_" & Format (Date, "yyyy-mm-dd")
    9.     ActiveWorkbook.SaveAs strFileName
    10.  End Sub


    i dont know if you need to include the file extension in the name aswell or if you can get away with out it, but if you need it just change:-

    strFileName = "MyFilePrefix_" & Format (Date, "yyyy-mm-dd")

    TO:

    strFileName = "MyFilePrefix_" & Format (Date, "yyyy-mm-dd") & ".xls"

Posting Permissions

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



Click Here to Expand Forum to Full Width