Newbie, how to save file with date and time as filename

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to be able to save a file with the name of the file to be the
current date and time.
Any help?
 
one way, assuming a date is in a1 using =now()
Sub test()

Dim wb As String
Dim fPath As String
Dim dtStr As String
Dim fName As String
Dim ws As Worksheet

Set ws = Worksheets("sheet1")
wb = ThisWorkbook.Name
fPath = ThisWorkbook.Path & "\"
fName = Format(ws.Range("A1").Value, "mmddyy hhmm")
Workbooks(wb).SaveAs Filename:=fPath & fName & ".xls"
End Sub
 
Micheal,

This works for me in project i'm currently working on. You can use a command
button to activate it if you like -

CommandButton1_Click()

ActiveWorkbook.SaveCopyAs "C:\{My FolderName}\{My FileName} _
& "." & Format(Now, "dd-mmm-yyyy.hh-mm-ss") & ".xls"

End Sub

Replace the bracketed {} with your own folder and file names.

hth
Mark
 
Mark,
Thats cool, I got it to run as a macro without the button thing (dont get
that) but it saves a copy but not the one I am working in.
So if I am working on book1 it will save a copy with the date and time but
it will still say book1.
This might just be a small tweak to do so I thought I would ask some more.
THANKS
 
Cool, dont get the button part but got the rest to work. Changed it to saveas
instead of copy.
THNAKS
 
I like this better.
ActiveWorkbook.SaveCopyAs "C:\{My FolderName}\{My FileName} _
& "." & Format(Now, "yyyy-mm-dd.hh-mm-ss") & ".xls"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top