VBA Excel file to CSV, keeps CSV filename same as original workbook

前端 未结 3 1856
鱼传尺愫
鱼传尺愫 2021-01-27 15:36

I am trying to find a fast way to save my xlsx files as csv files with the same file-name as the xlsx file (just in csv forma

3条回答
  •  佛祖请我去吃肉
    2021-01-27 16:02

    Before saving as csv, get the name of the xls file. You can use the ActiveWorkbook.Nameproperty. Assuming that file is called something.xls(and not .xlsx), try this:

    Sub Macro1()
        XLSName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
        ActiveWorkbook.SaveAs Filename:="C:\Users\Username\Desktop\" & XLSName & ".csv", _
            FileFormat:=xlCSV, CreateBackup:=False
    End Sub
    

    This pulls the workbook name, cuts off the last 4 characters (".xls") and then runs the Save As command appending ".csv" to that. In case your Excel file has the xlsx extension, change line 2 to:

    XLSName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
    

    Let me know if this works for you.

提交回复
热议问题