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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,910
Office Version
  1. 365
Platform
  1. Windows
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

mole999

Well-known Member
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
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

Jimmy P

New Member
Joined
Aug 23, 2014
Messages
45
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

Jimmy P

New Member
Joined
Aug 23, 2014
Messages
45
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

mole999

Well-known Member
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
just say thanks, threads stay live
 
Upvote 0

Forum statistics

Threads
1,190,678
Messages
5,982,220
Members
439,769
Latest member
trungminh2802

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
Top