简体   繁体   中英

Save the file to the corresponding folder based on month and year

I create a new file every day that is named based on the previous business day It looks like "mmddyyyy ENCORE and Floor". It's a csv file and I need to convert it to xlsm

This code successfully saves my file with the correct name and file type but I need it to save to a different place on my computer with folders based on months:

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

The correct file path looks like this: C:\Users\Sarajevo2022\Company Name\Coworker - OCC ENCORE\2022\Dec 2022

Any direction?

Save As Macro-Enabled Workbook

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM