1
votes

I want to save the current xlsm file as xlsx, so i wrote a code as below. This code really done it job and i able to see the file saved as Myfile.xlsx as what i've defined in the code, however there is some small issues where the vba always save the file to another file as bookx.xlsx beside the Myfile.xlsx. How can i avoid this ?

Sub saveAsXlsx()
Dim myPath As String
myPath = Application.ActiveWorkbook.Path
Worksheets(Array("sheet1", "sheet2")).Copy
ActiveWorkbook.SaveCopyAs Filename:=myPath & "\Myfile.xlsx" 
End Sub
1
It does not create another file, it just opens a new workbook with that name (BookX.xlsx). You just need to close that window after your script.ttaaoossuuuu
how can i close that window ?user1902849
Activeworkbook.Close Falsettaaoossuuuu

1 Answers

2
votes

This will avoid the extra copy:

Sub saveAsXlsx()
    Dim myPath As String
    myPath = Application.ActiveWorkbook.Path
    Worksheets(Array("sheet1", "sheet2")).Copy
    ActiveWorkbook.SaveAs Filename:=myPath & "\Myfile.xlsx"
    ActiveWorkbook.Close
End Sub

This uses SaveAs rather than SaveCopyAs since you already have created the first copy.