简体   繁体   中英

Save XLS to XLSM

When running a macro, Excel runs out of rows since it's in 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. It should have the same name as the XLS.

My macro is in a different Excel workbook. I type in "C2" the path of the Excel workbook I want to work with and then run the macro.

I have the following code for saving as XLSX, but it gives me an error:

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

The error is:

Run-time error '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.**

Try using the same name as the original but let the FileFormat argument determine the file extension.

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. Use XLSM or XLSB file format for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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