Saving a file with date & time from sub

Jimmy P

New Member
Joined
Aug 23, 2014
Messages
45
<!--[if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--> I'm trying save my workbook after processing with the contents of a worksheet cell (cell has formula =Now() ) appended to the File Name. I've tried everything that I could find on his forum and from all over the web with no success.

Here's the code as is. I get Run-time Error "1004" as a return.

Sub SaveWithDateTime()
ActiveWorkbook.SaveAs Filename:= "C:\Users\Admin1\Desktop\Working Copies\test Working Copy.xlsm" & _ Sheets("Calc").range("i1"), _ FileFormat:= xlOpenXMLWorkbookMacroEnabled, CreateBackup:=True
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You need that value before your extension and you typically want to remove all the special characters from your date (such as /,:).
You can use the FORMAT function to format the date/time any way you want, i.e.
Code:
[COLOR=#333333]Sub SaveWithDateTime()[/COLOR]
[COLOR=#333333]    ActiveWorkbook.SaveAs Filename:= "C:\Users\Admin1\Desktop\Working Copies\test Working Copy_" & _ 
        Format(Sheets("Calc").Range("i1"),"yyyymmdd_hhmmss") & ".xlsm", _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=True[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
Note, you also don't need to store the current date/time in a cell on your Excel sheet to use it. You can call NOW() directly from VBA, i.e.
Code:
[COLOR=#333333]Sub SaveWithDateTime()[/COLOR]
[COLOR=#333333]    ActiveWorkbook.SaveAs Filename:= "C:\Users\Admin1\Desktop\Working Copies\test Working Copy_" & _ 
        Format(Now(),"yyyymmdd_hhmmss") & ".xlsm", _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=True[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Working Copies\test Working Copy.xlsm" & _ Sheets("Calc").range("i1"),

needs to be like this

Working Copies\test Working Copy Sheets("Calc").range("i1").xlsm" &

so the date is inside the name and extension

this is only a demo of something close
 
Upvote 0
Good Golly...That was fast. Works great. Used the second one since I really don't need to use the cell contents with it. Goes in my bag of tips.

"It's all easy when you know how"

Much thanks.
 
Upvote 0
Since you're a moderator. How do I mark a thread as "solved" or complete or something to indicate it's no longer active after a solution has been found.
 
Upvote 0
just say thanks, threads stay live
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,727
Members
449,049
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top