Save as different file type

核能气质少年 提交于 2021-02-08 07:14:10

问题


I would like to make my macro able to save a xlsx file as csv exactly with the same name when running it.

This is what I tried:

ActiveWorkbook.saveas Filename:=ActiveWorkbook.Path & "\" & _
    ActiveWorkbook.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False

However, it saves the file as .xlsx.csv (i.e, a file called prices.xlsx is saves as prices.xlsx.csv) How could I save the file with a different file extension, without the .xlsx?


回答1:


If you want to make it more failsafe, in case your extension may be xls, xlsm, xlsb, you can do something like this:

Dim parts As Variant

parts = Split(ActiveWorkbook.Name, ".")
parts(UBound(parts)) = "csv"

ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & _
    Join(parts, "."), FileFormat:=xlCSV, CreateBackup:=False

It's probably not 100% bulletproof, although I'm struggling to think of a situation where it would not work as expected.




回答2:


Filename := ActiveWorkbook.Path & "\" & Replace(ActiveWorkbook.Name,".xlsx", ".csv")


来源:https://stackoverflow.com/questions/34618668/save-as-different-file-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!