Thursday, April 29, 2010

How do I save an Excel file with a macro?

There may be times when you will want a macro to save a file automatically after it is run. The second macro will save the file with a name called "MySavedFile". You may specify the path if you need to. The last macro saves all opened workbooks.

Sub SaveFile()
ActiveWorkbook.Save
End Sub

Sub SaveName()
ActiveWorkbook.SaveAs Filename:="C:\MySavedFile.xls"
End Sub

Sub SaveAll()
myFile = ActiveWorkbook.Name
    ActiveWorkbook.Save
    ActiveWindow.ActivateNext
Do While mySavedFile <> ActiveWorkbook.Name
    ActiveWorkbook.Save
    ActiveWindow.ActivateNext
Loop
End Sub