stewie
stewie

Reputation: 1

Export a workbook in this format: "YYYY-MM-DD"_filename.csv

I want to save a workbook in excel in this format : YYYY-MM-DD_filename.csv

It seems that the problem is in the line:

InitFileName = ThisWorkbook.Path &  Format(Date, "YYYY-MM-DD") & "\_filename" 

When I put it like this :

InitFileName = ThisWorkbook.Path & "\_filename" &  Format(Date, "YYYY-MM-DD") 

it works, but the date is at the end of the filename. This is my code in VBA:

Sub Export_CSV() 'Save as CSV 

    Application.DisplayAlerts = False
    Sheets("Sheets1").Activate
    Dim wb As Workbook, InitFileName As String, fileSaveName As String
    
    
    
    
    InitFileName = ThisWorkbook.Path &  Format(Date, "YYYY-MM-DD") & "\_filename" 

    Sheets("Sheets1").Copy

    Set wb = ActiveWorkbook

    fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitFileName, _
    fileFilter:="CSV(Trennzeichen-getrennt)  (*.csv), *.csv")

    With wb
        If fileSaveName <> "False" Then

            .SaveAs fileSaveName, FileFormat:=xlCSV, Local:=True
            .Close
        Else
            .Close False
            Exit Sub
        End If
    End With

End Sub

Upvotes: 0

Views: 109

Answers (1)

braX
braX

Reputation: 11735

Your backslash is just in the wrong place - try it like this

InitFileName = ThisWorkbook.Path & "\" & Format(Date, "YYYY-MM-DD") & "_filename"

Upvotes: 1

Related Questions