另存为不同的文件类型 [英] Save as different file type
问题描述
我想让我的宏在运行时能够以完全相同的名称将 xlsx 文件另存为 csv.
I would like to make my macro able to save a xlsx file as csv exactly with the same name when running it.
这是我试过的:
ActiveWorkbook.saveas Filename:=ActiveWorkbook.Path & "\" & _
ActiveWorkbook.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
但是,它会将文件保存为 .xlsx.csv(即,名为prices.xlsx 的文件保存为prices.xlsx.csv)如果没有 .xlsx,如何使用不同的文件扩展名保存文件?
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?
推荐答案
如果你想让它更安全,以防你的扩展可能是 xls、xlsm、xlsb,你可以这样做:
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
它可能不是 100% 万无一失的,尽管我很难想象它不会按预期工作的情况.
It's probably not 100% bulletproof, although I'm struggling to think of a situation where it would not work as expected.
这篇关于另存为不同的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!