Consulting

Results 1 to 6 of 6

Thread: Creating excel file using VBA - File name to contain the current date and time

  1. #1

    Creating excel file using VBA - File name to contain the current date and time

    Hi all,

    I am running a VBA based tool that I have made which calculate certain output.

    Now, the problem is I run this tool multiple times a day and I would like to save each output in a separate excel file.

    Is it possible to write a macro to create a separate excel file that contains current system date and time and export my output to that file each time so that I have each version of output saved.

    If yes, can somebody share the code for such an exercise. I have searched online a bit, but with no use.

    Thanks in advance.

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    something like this?
    Sub CreateDateTimeFileName()
        Dim sFileName As String
        
        sFileName = Format(Now(), "mmddyyyy") & " - " & Format(Now(), "hhss")
        
        MsgBox sFileName
    End Sub

  3. #3
    Hello Jkwan,

    Not sure exactly you looking for but:

    Here is how I have it up in my MSaccess vba---> strFile = "C:\Users\USERNAME\Desktop\Reports\FileNAME-" & Format(Date, "mm-dd-yyyy") & ".xls"

    When I run my report it outputs the file name including todays timestamp into my directory.

    fill in your user name and file name extension.

    Pasi.

  4. #4
    Quote Originally Posted by JKwan View Post
    something like this?
    Sub CreateDateTimeFileName()
        Dim sFileName As String
        
        sFileName = Format(Now(), "mmddyyyy") & " - " & Format(Now(), "hhss")
        
        MsgBox sFileName
    End Sub
    Thanks JKwan.

    But, where is the file getting created? I can't find any excel file on my system with that name. The message box is appearing though

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Where is the code going to be housed? In the workbook that you want multiple copies of?

    Also - what year of Excel are you using, and what format do you save in (.xls, .xlsx, .xlsm, ...)?

  6. #6
    Dear all,

    Thanks for the help on this. I have figured out how to do this.

    Regards

Posting Permissions

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