excel 另存为昨天的日期,不带宏

jogvjijk  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(179)

我有VBA代码将我的文件保存在与我想要的文件名&昨天日期相同的文件夹中。
我用today()函数在U8单元格中写入文件名(“mylenametoday()-1”)。我不能用VBA代码来完成,总是出现错误400。
我不希望在这个新保存的文件中有任何宏,因为它必须是xlsx。
我添加了代码将新保存的文件格式更改为.xlsx,但打开文件时出错,因为文件中包含宏且格式错误。
我也尝试了StackOverflow的一些其他结果,但没有一个对我有效。

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
6yjfywim

6yjfywim1#

请尝试以下代码:

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

相关问题