1004 error & ldquo; Is it possible that it has been moved, renamed or delete...
source link: https://www.codesd.com/item/1004-error-is-it-possible-that-it-has-been-moved-renamed-or-deleted.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
1004 error & ldquo; Is it possible that it has been moved, renamed or deleted? & Rdquo;
My goal: make Excel save a copy of the workbook in a folder different from the original folder where the workbook is and as well with a timestamp.
This code runs correctly:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.SaveCopyAs Replace(ThisWorkbook.FullName, ".xlsm", " _
(" & Format(Now, "yyyy-mm-dd hhmm") & ").xlsm")
End Sub
But this code doesn't run at all. I get Error 1004 every time I close the workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.SaveCopyAs (ThisWorkbook.Name & Format(Now, "yyyy-mm-dd hh:mm"))
End Sub
The message I get when trying to close the workbook with the second code: http://prntscr.com/8iiyyd
It is the colon that prevents Windows from saving the file, plus the extension will be strange with the timestamp added like this. Your code would produce the following filename: Try.xlsm2015-09-22 15:37
You will also need to supply your folder location.
Maybe you can try this:
ThisWorkbook.SaveCopyAs "c:\folder\" & Replace(ThisWorkbook.Name, ".xl", Format(Now(), "yyyy-mm-dd hhmm") & ".xl")
You can of course specify your own folder.
If you want to put it in a subfolder, relative to the current file location, then use this:
ThisWorkbook.SaveCopyAs Thisworkbook.Path & "\subfolder\" & Replace(ThisWorkbook.Name, ".xl", Format(Now(), "yyyy-mm-dd hhmm") & ".xl")
You can of course specify your own subfolder
Hope this helps
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK