简体   繁体   English

将XLS保存到XLSM

[英]Save XLS to XLSM

When running a macro, Excel runs out of rows since it's in XLS. 运行宏时,Excel不在XLS中,因此行用完了。

I want at the beginning to save the file as an XLSX and then keep the macro running on the XLSX file just saved. 我想在一开始将文件另存为XLSX,然后使宏在刚刚保存的XLSX文件上运行。 It should have the same name as the XLS. 它应该与XLS具有相同的名称。

My macro is in a different Excel workbook. 我的宏在另一个Excel工作簿中。 I type in "C2" the path of the Excel workbook I want to work with and then run the macro. 我键入“ C2”要使用的Excel工作簿的路径,然后运行宏。

I have the following code for saving as XLSX, but it gives me an error: 我有以下代码另存为XLSX,但这给我一个错误:

aWbookIworkWith.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook

The error is: 错误是:

Run-time error '1004': 运行时错误“ 1004”:
The file could not be accessed. 无法访问该文件。 Try one of the following: 请尝试以下方法之一:
• Make sure the specified folder exists. •确保指定的文件夹存在。
• Make sure the folder that contains the file is not read-only. •确保包含文件的文件夹不是只读的。
• Make sure the file name does not contain any of the following characters: < > ? •确保文件名不包含以下任何字符:<>? [ ] : | []:| or * 要么 *
• Make sure the file/path name doesn't contain more than 218 characters.** •确保文件/路径名所包含的字符数不超过218个。**

Try using the same name as the original but let the FileFormat argument determine the file extension. 尝试使用与原始名称相同的名称,但让FileFormat参数确定文件扩展名。

dim myFileName as string

if lcase(right(aWbookIworkWith.fullname, 4)) = ".xls" then
    myFileName = left(aWbookIworkWith.fullname, len(aWbookIworkWith.fullname)-4)
    aWbookIworkWith.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbook
else
    debug.print "not an XLS workbook"
end if

Of course, the VBA project that contains this code should not be in the same workbook as aWbookIworkWith since an XLSX cannot contain a VBA project. 当然,包含此代码的VBA项目不应与aWbookIworkWith放在同一工作簿中,因为XLSX不能包含VBA项目。 Use XLSM or XLSB file format for that. 为此,请使用XLSM或XLSB文件格式。

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

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