Discussion Locked

Saving an Excel file

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I want to create a macro to save an Excel file, using the contents of a cell as the file name. Can anyone help please?

9 replies

by kees.valkenswaard

An example would be the following VBA-code:
Sub SaveXLS()
Dim StrExName As String
Range("D1").Select
StrExName = Selection.Value
ActiveWorkbook.SaveAs Filename:= _
StrExName & ".xls", FileFormat:=xlNormal _
, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
End Sub
This assumes the name in cell D1.
It will save in the current (default) directory and assumes that the name would be without the extension .xls whichis added in the save. Elaboration is always possible.
A simple way to get this would be to record a very short macro with the recorder. Edit it and replace all text with the code above.
I do not know if you want to have it in a template or in a different way. You could connect it to a button added to one of the toolbars of the template with customize toolbars..
Look under the category all macros.
The _ would have to be at the end of a line, so some editing may be needed.
Good luck

by gxh

Answer 1 works great. I routinely use a similar procedure to "build" file names. However, I am having trouble setting the default directory. Any idea how to do this?

This discussion is closed to new replies.