SAVE OR SAVE-AS with a file name derived from a cell on the spreadsheet...

  • Thread starter Thread starter KLZA
  • Start date Start date
K

KLZA

Hi. I'd like to run a macro that executes on save or save-as and
automatically uses the filename from a cell on the spreadsheet. Is
this possible?
 
SaveAs

I don't know how to run a macro from Save or SaveAS. But the following codes can be assigned to a button. Maybe these will help

Sub SaveCopyAs()
'SaveAs will save the current file and change its name closing the origional file and opening the new
'SaveCopyAs will save the current file and change its name and leave open the origional file not opening the new
'The code can be written 'SaveCopyAs' or 'SaveAs' depending on what you want to do.
'This code Saves the file with whatever name you put in the active cell _
you must put file extention in cell. The file is saved in the same folder as origional file
ActiveWorkbook.SaveCopyAs FileName:=ActiveCell.Value
End Sub

I suspect the following code is more what you want.
The concatenation can be changed to whatever you want.

Sub SaveCopyAsA(
'You can write 'SaveAs' or 'SaveCopyAs' depending on what you want to do.
''This code Saves the file with whatever name you put in the Range("A1") _
plus a space plus .xls file extention. The file is saved in the same folder as origional file
ActiveWorkbook.SaveCopyAs FileName:=Range("A1").Value & " " & ".xls"
End Sub

Sub SaveASSS()
''fileSaveName fileSaveNameAs either works
''This Code saves the file with a file name initially proposed _
in save file dialog box as the value of "A1".
Dim fileSaveNameAs
fileSaveNameAs = Application.GetSaveAsFilename( _
InitialFileName:=Range("A1").Value, _
fileFilter:="Microsoft Excel Files (*.xls), *.xls")
If fileSaveNameAs <> False Then
MsgBox "Save as " & fileSaveNameAs
End If
End Sub

'The following code does the same as all the above
Sub SaveAS()
Application.Dialogs(xlDialogSaveAs).Show
End Sub
Hope this is helpful and not too late, I only just saw your question.
GeoffreyBarnard
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top