【问题标题】:SaveAs with yesterday date and without macrosSaveAs with yesterday date and without macros
【发布时间】:2022-12-27 22:25:54
【问题描述】:

I have VBA code to save my file in the same folder with my file name I want & yesterday date.

I write file name in cell U8 with today() function ("myfilename today()-1"). I couldn't do it by VBA code, always had error 400.

I don't want any macros in this new saved file because it must be in xlsx format.
I added code to change the new saved file format to .xlsx but it results in an error opening the file because it has macros and the format is wrong.
Also tried some other results from StackOverflow but none worked for me.

Dim path As String

Dim filename1 As String

path = ThisWorkbook.path & "\"     'Same path as current project that the User opened.

filename1 = Range("U8").Text

Application.DisplayAlerts = False

ActiveWorkbook.SaveCopyAs Filename:=path & filename1

Application.DisplayAlerts = True

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    Try this code:

    Sub SubMacroFreeSave()
        
        'Declarations.
        Dim path As String
        Dim filename1 As String
        Dim file As Variant
        
        'Settings path as current project that the User opened.
        path = ThisWorkbook.path & ""
        
        'Setting filename1 as name of the current project that the User opened followed by the yesterday's date.
        filename1 = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - Len(Split(ThisWorkbook.Name, ".")(UBound(Split(ThisWorkbook.Name, ".")))) - 1) & " " & Excel.WorksheetFunction.Text(Now - 1, "dd-mm-yyyy")
        
        'Turning off the screen updating.
        Application.ScreenUpdating = False
        
        'Turning off the display alerts.
        Application.DisplayAlerts = False
        
        'Saving a copy of the file as xlsm.
        ActiveWorkbook.SaveCopyAs Filename:=path & filename1 & ".xlsm"
        
        'Setting file as the copy just created.
        Set file = Application.Workbooks.Open(path & filename1 & ".xlsm")
        
        'Saving the copy again as a macro-free file.
        file.SaveAs Filename:=path & filename1 & ".xlsx", _
                    FileFormat:=xlWorkbookDefault, _
                    CreateBackup:=False
        
        'Deleting the first copy of the file.
        Kill path & filename1 & ".xlsm"
        
        'Closing the file (the macros will still be avaiable until the file is closed).
        file.Close
        
        'Turning on the display alerts.
        Application.DisplayAlerts = True
        
        'Turning on the screen updating.
        Application.ScreenUpdating = True
        
    End Sub
    

    【讨论】:

    • everything working fine, thanks a lot! now i just need to learn from it to do it by myself in future:)
    • one more question, is there any possible to hide msgbox while saving new file? because ive got one msgbox on workbook_open and when its saving by this great macro its need to click OK to go further and close the .xlsm copy (which is later deleted) - maybe you got some advice on it?
    • If you have an actual MsgBox, i don't think you can save (or do anything else) a file while it's displayed. The code stops on the MsgBox. You might use a fake MsgBox using a form made by yourselves.
    猜你喜欢
    • 2017-12-07
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2022-12-02
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    相关资源
    最近更新 更多