简体   繁体   English

运行时错误 1004

[英]Run time error 1004

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." “此扩展名不能与所选文件类型一起使用。通过更改另存为类型更改文件名文本框中的文件扩展名或 select 不同的文件类型。”

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

The error means that the ActiveWorkbook is trying to save as a different file format then ".xlsx".该错误意味着 ActiveWorkbook 正在尝试另存为不同的文件格式,然后是“.xlsx”。 To force it to save as.xlsx, you also have to pass the fileformat.要强制它另存为.xlsx,您还必须传递文件格式。

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

I had the same problem when I was trying to convert a macro enabled workbook (xlsm) into a normal workbook (xlsx)!当我尝试将启用宏的工作簿 (xlsm) 转换为普通工作簿 (xlsx) 时,我遇到了同样的问题! I finally gave up using the ActiveWorkbook.SaveAs method and used the following code instead:我最终放弃了使用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 .代码最初来自这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM