View Full Version : VBA Date prob


maw230
12-31-2009, 08:41 AM
ActiveWorkbook.SaveAs Filename:= _
"G:\DAILY REPORTS\2009\12 DECEMBER\Product Code\PLCD Daily Change Report" & " " & Month("T1") & "-" & Day("T1") & "-" & Year("T1"), FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False

this is my code to return today's date in the title of the Excel file when it is saved. T1 is a date field.. this gave me a data type error.
ive tried other ways with no luck. such as using the Date function.
basically, how to i get todays date to save in the title?

boblarson
12-31-2009, 08:47 AM
Are you doing this in Access or in Excel?

Second, you are not using the functions right. If you are referring to a cell (T1) on the Excel sheet then you can't use Month("T1") as that is invalid. You would need something like Month(ActiveSheet.Range("T1")).

Third, you are missing the file extension on the end - like ".xls" or ".xlsx" etc.

And if this is done in Access you could use

Format(xlApp.ActiveSheet.Range("T1").Value, "mm-dd-yyyy") & ".xls"

etc.

And if this IS done in Access you should post your whole code because it would appear that you are using incomplete code for referring to Excel objects which can then leave a hidden Excel instance hanging around until you close Access or kill it in the Task Manager. And that can wreak havoc on you if you want to run your code a second time.

maw230
12-31-2009, 08:50 AM
bob, im using excel. id rather not use a cell reference but instead use a simple date function, but my attempts at that havent worked. i will try the cell reference again with the revised code

boblarson
12-31-2009, 08:56 AM
bob, im using excel. id rather not use a cell reference but instead use a simple date function, but my attempts at that havent worked. i will try the cell reference again with the revised code

If you just want today then you can use

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

or if 2007

Format(Date(),"mm-dd-yyyy") & ".xlsx"

or if 2007 and the workbook has code in it:

Format(Date(),"mm-dd-yyyy") & ".xlsm"

maw230
12-31-2009, 08:57 AM
thanks again. the revised cell reference works just fine. happy new year!

boblarson
12-31-2009, 08:57 AM
Also, I moved this to the Excel category where it should have been.