vba 如何另存为xlsx文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37570620/
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-08 10:27:10  来源:igfitidea点击:

How to save as xlsx file?

excelvbasave-as

提问by yarz-tech

My SaveFile sub saves the file but when I try to open it again Excel doesn't recognize it as an Excel file.

我的 SaveFile 子保存文件,但是当我尝试再次打开它时,Excel 无法将其识别为 Excel 文件。

If I right click the file from my desktop and check the properties, the type of file is "File". I've read up on the formatting but can't get this file to save as an xlsx format. I was able to get a macro-enabled excel file to work properly but that's not what I want.

如果我从桌面右键单击文件并检查属性,则文件类型为“文件”。我已经阅读了格式,但无法将此文件另存为 xlsx 格式。我能够让启用宏的 excel 文件正常工作,但这不是我想要的。

Sub SaveFile()

    MsgBox ("You will now be prompted to save your file") 'Notifies User 
    savename = Application.GetSaveAsFilename()  'Gets directory/name
    ActiveWorkbook.SaveAs Filename:=savename, FileFormat:=51 'Something is wrong 

End Sub

Here is the picture of the "Formatless" file
enter image description here

这是“无格式”文件的图片
在此处输入图片说明

回答by some_weired_user

When saving the file you should save it with its extension:

保存文件时,您应该使用其扩展名保存它:

Sub SaveFile()

    savename = Application.GetSaveAsFilename(fileFilter:="Exel Files (*.xlsx), *.xlsx")) 
    ActiveWorkbook.SaveAs Filename:=savename, FileFormat:=51 

End Sub