I have a code which use SaveCopyAs method to change name file and the format to xlsx. My problem is, I think, because I change format type from xls to xlsx. When I open the new file a error message pop-up : Impossible to open this file because its format or extension is not valid….
My goal is to trigger a save as adding automatically a file name (this is working) and change format to xlsx
Sub Save()
Dim nameFile as String
Dim pathDest as String
nameFile = Cells(2, 18).Value
pathDest = ThisWorkbook.Path & "\"
ThisWorkbook.SaveCopyAs pathDest & nameFile & ".xlsx"
End Sub
Sub Save()
Dim nameFile as String
Dim pathDest as String
On Error GoTo ExitErr
Application.DisplayAlerts = False
nameFile = Cells(2, 18).Value
pathDest = ThisWorkbook.Path & "\"
ThisWorkbook.SaveAs pathDest & nameFile & ".xlsx", FileFormat:=51
ExitErr:
Application.DisplayAlerts = True
End Sub
This will set both the file extension and the file format correctly, as well as disabling the prompt you’ll get when doing that.
Answer:
Apologies if I’m oversimplifying this due to an incorrect copy & paste, but you need to end the apostrophe marks on your “.xlsx
Another few points to try:
- Use the function ActiveWorkbook.SaveCopyAs instead of ThisWorkbook
- Define your pathDest as the function Application.ActiveWorkbook.Path
Tags: excelexcel, file, vba