工作簿beforesave宏

时间:2016-03-08 11:09:33

标签: excel

尝试使用工作簿beforesave宏来保存带有日期修订号和时间的文件名。要使用新名称“BOOK1 - 2016年3月8日 - Rev 001 08-59.xlsx”保存文件,或者如果工作簿是最终启用宏的.xlsm。到目前为止,这是编码,但它似乎没有工作或做它应该做的事情。任何建议表示赞赏。

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.EnableEvents = False

    Dim file_name As Variant
    Dim strDate As String
    Dim strVer As String

    ' Get the file name.
    file_name = Application.GetSaveAsFilename( _
        FileFilter:="Excel Files,*.xlsx,Macro EnabledWorkbook,*.xlsm, All Files,*.*", _
        Title:="Save As File Name")

    ' check if the user has canceled.
    If file_name = False Then Exit Sub

    strDate = Format((Date), "dd MMM yyyy")
    strVer = "0"
    strVer = Val(strVer) + 1 'Increment number



    ' Save the file with the new name.
    If LCase$(Right$(file_name, 4)) <> ".xls" Then
        file_name = file_name  & " - " & strDate & _
    " - Rev " & Format(Val(strVer), "00# ") _
    & Format(Time(), "hh-mm") & ".xls"
    End If

    ActiveWorkbook.SaveAs Filename:=file_name
   End Sub 

0 个答案:

没有答案