我一直在玩以下代码。它第一次工作,但随后它在已经加时间戳的文件名上添加了另一个时间戳。我希望在同一目录中创建一个格式为2018 Test_YYYY.MM.DD_HHMM
的新文件名,每次更新时间戳。如果我能弄清楚如何添加修改文件的最后一个用户,那就获得奖励!
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sFileName As String
Dim sDateTime As String
With ThisWorkbook
sDateTime = " (" & Format(Now, "yyyy-mm-dd hhmm") & ").xlsm"
sFileName = Application.WorksheetFunction.Substitute _
(.FullName, ".xlsm", sDateTime)
.SaveCopyAs sFileName
End With
End Sub
答案 0 :(得分:2)
这应该确定您是否已经在文件名中添加了日期时间。
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sDateTime As String, sFileName As String
With ThisWorkbook
sDateTime = " (" & Format(Now, "yyyy-mm-dd hhmm") & ").xlsm"
If Right(LCase(.FullName), 6) = ").xlsm" Then
sFileName = Left(.FullName, Len(.FullName) - Len(sDateTime)) & sDateTime
Else
sFileName = Left(.FullName, InStrRev(.FullName, ".") - 1) & sDateTime
End If
.SaveCopyAs sFileName
End With
End Sub