excel 根据月份和年份将文件保存到相应的文件夹

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

我每天创建一个新文件,该文件基于上一个工作日命名,看起来像“mmddyyyy ENCORE and Floor”。这是一个csv文件,我需要将其转换为xlsm
这段代码成功地保存了我的文件与正确的名称和文件类型,但我需要它保存到我的计算机上的文件夹基于月份的不同地方:

ActiveWorkbook.SaveAs Filename:="C:\Users\Sarajevo2022\Downloads\" & _
Format(Evaluate("Workday(today(),-2)"), "mmddyyyy") & _
" ENCORE and Floor", FileFormat:=52

正确的文件路径如下所示:C:\用户\萨拉热窝2022\公司名称\同事- OCC ENCORE\2022\2022年12月
任何方向?

icnyk63a

icnyk63a1#

保存为启用宏的工作簿

Sub SaveAsMacroEnabled()
    
    ' Build the folder path.
    
    Dim FolderLeft As String: FolderLeft = "C:\Users\Sarajevo2022"
    ' or:
    'Dim FolderLeft As String: FolderLeft = Environ("USERPROFILE")
    Dim FolderMid As String: FolderMid = "\Company Name\Coworker - OCC ENCORE\"
    
    Dim SaveDate As Date: SaveDate = Application.WorkDay(Date, -2)
    
    Dim FolderRight As String: FolderRight = Format(SaveDate, "yyyy") _
        & "\" & UCase(Format(SaveDate, "mmm yyyy")) & "\"
    Dim FolderPath As String: FolderPath = FolderLeft & FolderMid & FolderRight
    
    ' Check if the folder path exists.
    
    Dim PathExists As Boolean
    
    With CreateObject("Scripting.FileSystemObject")
        PathExists = .FolderExists(FolderPath)
    End With
    
    If Not PathExists Then
        MsgBox "The path '" & FolderPath & "' doesn't exist!" _
            & vbLf & vbLf & "File not saved!", vbCritical
        Exit Sub
    End If
    
    ' Build the file path.
    Dim FilePath As String: FilePath = FolderPath _
        & Format(SaveDate, "mmddyyyy") & " ENCORE and Floor" & ".xlsm"
    
    ' Return the paths in the Immediate window (Ctrl+G).
    Debug.Print FolderPath & vbLf & FilePath
    
    ' After you have confirmed that the paths are correct,
    ' out-comment the previous and uncomment the next line.
    'ActiveWorkbook.SaveAs FilePath, xlOpenXMLWorkbookMacroEnabled ' or 52
    
End Sub

相关问题