vba VB 保存到当前路径和文件名,加上当前日期到文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16364081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB Save to current path and filename, plus add current date to filename
提问by AMM
I am very, very new to VB and am grateful to have found this site!
我对 VB 非常非常陌生,很高兴找到了这个网站!
I searched and reviewed every auto-suggested similar Q&A links for my question but didn't find exactly what I was looking for (perhaps because I didn't understand what I was looking at... ;-).
我搜索并查看了我的问题的每个自动建议的类似问答链接,但没有找到我正在寻找的内容(也许是因为我不明白我在看什么......;-)。
I used this Excel macro from another user's question, answered by Siddharth Rout:
我从另一个用户的问题中使用了这个 Excel 宏,由Siddharth Rout回答:
Sub Save()
Dim FilePath As String
Dim NewName As String
FilePath = "X:\": NewName = FilePath & "file" & Format(Date, "MM-DD-YYYY") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=NewName, FileFormat _
:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
This works perfectly if I want the file to be named "file" (or whatever I insert in the quotes).
如果我希望将文件命名为“文件”(或我在引号中插入的任何内容),这非常有效。
But, how would I edit this macro so that it would work on any open file so that the current date is added to the file's existing path and filename?
但是,我将如何编辑这个宏,使其适用于任何打开的文件,以便将当前日期添加到文件的现有路径和文件名中?
Thanks in advance for your help!
在此先感谢您的帮助!
Andy
安迪
回答by GSerg
dim last_dot as long
last_dot = InStrRev(ActiveWorkbook.FullName, ".")
dim NewName as string
NewName = Left$(ActiveWorkbook.FullName, last_dot - 1) & Format$(Date, "MM-DD-YYYY") & Mid$(ActiveWorkbook.FullName, last_dot)