我修改了一个例程,它执行您尝试执行的相同类型的操作,但使用文件的当前名称,而不是有 2 个保存对话框。
Option Explicit
Function SaveIt()
Dim CurrentFile As String
Dim FileExt As String
Dim GetFileName
CurrentFile = Left(ActiveWorkbook.FullName, InStrRev(ActiveWorkbook.FullName, ".") - 1)
FileExt = Mid(ActiveWorkbook.FullName, InStrRev(ActiveWorkbook.FullName, "."))
If InStr(CurrentFile, "_") Then
'has underscore
If InStrRev(CurrentFile, "_") = Len(CurrentFile) - 8 Then
' underscore 8 from end
If Right(CurrentFile, 8) = CStr(Val(Right(CurrentFile, 8))) Then
' and it's 8 digits at the end
CurrentFile = Left(CurrentFile, Len(CurrentFile) - 9)
'strip the end off
End If ' if it fails any of these tests,
End If 'then it's not got the underscore and date
End If ' and we don't touch the filename
CurrentFile = CurrentFile & "_" & Format(Now, "yyyymmdd")
GetFileName = Application.GetSaveAsFilename(CurrentFile & FileExt)
If GetFileName <> False Then 'Cancel returns false, otherwise it returns the filename
ActiveWorkbook.SaveAs GetFileName
End If
End Function
这也允许人们拥有名为test_1.xlsx和What_a_lot_of_underscores.xlsm的文件,而不必担心会破坏名称