问题
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