vba 运行时错误 1004

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

Run time error 1004

vbaexcel-vbaexcel

提问by john

When trying to run the below I get the following error:

尝试运行以下命令时,出现以下错误:

"This extension can not be used with the selected file type. Change the file extension in the file name text box or select a different file type by changing the save as type."

“此扩展名不能与选定的文件类型一起使用。在文件名文本框中更改文件扩展名或通过更改保存类型来选择不同的文件类型。”

Code:

代码:

Dim strPath As String
Dim strFolderPath As String


strFolderPath = "Y:\

strPath = strFolderPath & _
Sheet1.Range("A1").Value & _
Sheet1.Range("B1").Value & ".xlsx"


ActiveWorkbook.SaveAs Filename:=strPath

回答by mischab1

The error means that the ActiveWorkbook is trying to save as a different file format then ".xlsx". To force it to save as .xlsx, you also have to pass the fileformat.

该错误意味着 ActiveWorkbook 正在尝试另存为与“.xlsx”不同的文件格式。要强制将其另存为 .xlsx,您还必须传递文件格式。

ActiveWorkbook.SaveAs Filename:=strPath, FileFormat:=xlOpenXMLWorkbook

回答by Jing He

I had the same problem when I was trying to convert a macro enabled workbook (xlsm) into a normal workbook (xlsx)! I finally gave up using the ActiveWorkbook.SaveAsmethod and used the following code instead:

当我尝试将启用宏的工作簿 (xlsm) 转换为普通工作簿 (xlsx) 时遇到了同样的问题!我最终放弃了使用该ActiveWorkbook.SaveAs方法,而是使用了以下代码:

' Code from http://www.mrexcel.com/forum/excel-questions/516366-saving-xlsm-file-xlsx-using-visual-basic-applications.html#post4478019
sub saveAsXlsx
Dim mySheetList() As String
ReDim mySheetList(0 To (ThisWorkbook.Sheets.Count) - 1)
Dim a As Integer
a = 0
For Each ws In ActiveWorkbook.Worksheets
    mySheetList(a) = ws.Name
    a = a + 1
Next ws

'actually save
Worksheets(mySheetList).Copy
ActiveWorkbook.SaveAs fileName:=flenme 'default ext

The code is originally from here.

该代码最初来自此处