|
-
Oct 13th, 2004, 05:43 AM
#1
Thread Starter
New Member
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?
-
Oct 13th, 2004, 06:11 AM
#2
Member
You can use the function with to get the current date into the filename.
VB Code:
strFileName = "MyFilePrefix_" & Format(Date,"yyyy-mm-dd")
will return MyFilePrefix_2004-10-13.
-
Oct 14th, 2004, 04:50 AM
#3
Thread Starter
New Member
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?
-
Oct 14th, 2004, 05:41 AM
#4
You've tried to do 2 operations on the same line, change your code to read :-
VB Code:
Sub ExportSheet()
Cells.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
strFileName = "MyFilePrefix_" & Format (Date, "yyyy-mm-dd")
ActiveWorkbook.SaveAs strFileName
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|