vba 在vba中如何在保存文件时更改扩展名,不知道确切的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9200971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:10:49  来源:igfitidea点击:

In vba How to change extension when saving file, don't know exact filename

excelvbareplacefile-extension

提问by Brian

My issue is this.

我的问题是这个。

I read in a series of filenames using a wildcard, so that the end of the filename is unknown, and the extension is either .xls or .xlsx. So, the wildcard is something like :

我使用通配符读取了一系列文件名,因此文件名的结尾是未知的,扩展名是 .xls 或 .xlsx。所以,通配符是这样的:

beginningOfFilename_*.xls*

开始文件名_ *.xls*

I then want to take each file, after I have manipulated it, and save it with the same name, but as a .csv (comma seperated value file). In vba for excel, can I just specify the format and it will take care of the extension, or do I have to somehow pull off the( unknown) extension, and append .csv

然后我想获取每个文件,在我操作它之后,并使用相同的名称保存它,但作为 .csv(逗号分隔值文件)。在 excel 的 vba 中,我可以只指定格式并处理扩展名,还是我必须以某种方式取消(未知)扩展名,并附加 .csv

If the second case is neccessary how would you approach this problem, I don't know where to start, since part of the filename is unknown, and I am not sure how to manipulate strings in vba.

如果需要第二种情况,您将如何解决这个问题,我不知道从哪里开始,因为文件名的一部分是未知的,而且我不确定如何在 vba 中操作字符串。

I'm a VBA beginner.

我是 VBA 初学者。

Any help will be appreciated, thanks.

任何帮助将不胜感激,谢谢。

回答by Fionnuala

The line you want is :

你想要的线路是:

Mid(sFile, 1, InStrRev(sFile, ".")) & "csv"

Where sFile is the file name with any extension.

其中 sFile 是带有任何扩展名的文件名。

回答by Vatsa

Split(sFile, ".")(0) & ".csv"

where sFileis the filename

sFile文件名在哪里

回答by Bruno Leite

To get a path and name of your file without extension use.

要获取不带扩展名的文件的路径和名称,请使用。

Dim StrFileName as string

StrFileName= split(ThisWorkbook.fullName,".xls")(0)

Now save your Csv using StrFileName content.

现在使用 StrFileName 内容保存您的 Csv。

[]′s

[]的