Saving a workbook step in a Macro

M

Mark

I created a simple formatting macro and included the "save As" in the macro,
of course it wants to save the workbook as the name indicated in the macro.
Can I have the macro save the workbook as the current workbook name?
 
D

Dave Peterson

Are you saving the workbook with the code?
Do you want to overwrite the existing workbook?

If yes to both, you can use:
thisworkbook.save

If you want to save a different workbook with the same name as the workbook
that's running the code, then nope--you can't use Save|As.

You can only have one file open with any particular name.

There may be other work arounds.

You could save the file with a temporary name. Close that file. And then use
the Name statement to rename it to what you want.

There are other things to consider, too.

Is the workbook opened readonly?

Are you trying to use the same drive/path/filename or what????
 
M

Mark

The file is originally a .csv file that i am formatting and then saving as an
..xls file using the same file name, in the same folder and directory.
 
D

Dave Peterson

Note quite the same name (the extensions are different). So it's not a problem.

Option Explicit
Sub testme()
Dim myFileName As String
With ActiveWorkbook
'strip off the extension: .csv
myFileName = Left(.FullName, Len(.FullName) - 4)
'add the new extension: .xls
myFileName = myFileName & ".xls"
.SaveAs Filename:=myFileName, FileFormat:=xlWorkbookNormal
.Close savechanges:=False 'maybe???
End With
End Sub
 

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

Top