作者:绿色风
分类:
时间:2022-08-17
浏览:258
楼主 轩辕轼轲 |
Q:如何使用VBA在工作簿文件名称后添加文件保存时间的后缀,并且能不断的随着保存操作更新这个后缀? A:在Thisworkbook里面加入下面的代码即可实现,并且能更新后缀,使得我们一眼就能看出文件的最后保存时间。 方法一 By 轩辕轼轲
- Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
- On Error Resume Next
- Application.DisplayAlerts = False
- Fold = ThisWorkbook.FullName
- Fpath = ThisWorkbook.Path
- ThisWorkbook.Save
- Application.DisplayAlerts = False
- T = Format(Now, " YYYY年MM月DD日HH时MM分SS秒")
- If Mid(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4, 1) = "秒" Then
- Temp = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 25)
- Else
- Temp = Split(ThisWorkbook.Name, ".")(0)
- End If
- Fname = Fpath & "\" & Temp & T & ".xls"
- ThisWorkbook.ChangeFileAccess xlReadOnly
- Name Fold As Fname
- Application.Workbooks.Open Fname, False
- ThisWorkbook.Close False
- Application.DisplayAlerts = False
- Cancel = False
- End Sub
方法二 By KevinChengCW
- Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
- Dim mPath$, FN$, nName$
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Application.EnableEvents = False
- FN = ThisWorkbook.Name
- mPath = ThisWorkbook.Path
- With ThisWorkbook
- .ChangeFileAccess xlReadOnly
- If InStr(FN, "-Ver") > 0 Then
- .SaveAs mPath & "\" & Split(FN, "-Ver")(0) & "-Ver" & Format(Now, "yyyymmddhhmmss") & "." & Right(FN, Len(FN) - InStrRev(FN, ".")), ThisWorkbook.FileFormat
- Else
- .SaveAs mPath & "\" & Left(FN, InStrRev(FN, ".") - 1) & "-Ver" & Format(Now, "yyyymmddhhmmss") & "." & Right(FN, Len(FN) - InStrRev(FN, ".")), ThisWorkbook.FileFormat
- End If
- End With
- Kill mPath & "\" & FN
- Cancel = True
- Application.EnableEvents = True
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- End Sub
如何实现每次保存添加当前时间到文件 2011年02月20日12时32分09秒.rar |
2楼 xyf2210 |
学习 |
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一