Saving New File name in macro

Gman1950

New Member
Joined
Aug 27, 2002
Messages
15
I have a file with a macro, when done running the macro I need to save the file with a new name. I want to have the macro stay with the new file, so I can run the macro if needed. How can I automate the macro so when I do a "save as" command for the file the "new" file name gets appended to the macro so it will run properly ????
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

larai

New Member
Joined
Aug 28, 2002
Messages
20
If you are using VBA, all you need to do is use the reference "ThisWorkbook" to reference the current workbook.

The code you want would probably go something like:

Sub TestSaveAs()
ThisWorkbook.SaveAs Filename:=Application.GetSaveAsFilename, FileFormat:=xlWorkbookNormal
End Sub


if I am correctly understanding what you want to do.
 

larai

New Member
Joined
Aug 28, 2002
Messages
20
Oops,

Re-reading your post, I see that you are manually saving as the file.

You don't need to replace the code in your macro with the "new" filename.

If you are only using one workbook, the ThisWorkbook object will work.

If you are moving back and forth between multiple workbooks, then when you open the workbook with the macro saved in it, put in a line that will collect the filename and store it in a variable once the workbook opens.

I'd have to understand more what you are trying to do if these don't work out for you.
 

Gman1950

New Member
Joined
Aug 27, 2002
Messages
15
once the user saves the file as a different name I want the macro to recognize the new file name. the macro I have written switches back and forth between the saved file with the macro and a temp file. if the file name changes the macro crashes because it is still looking for the original file name/worksheet. you previously mentioned "put in a line that will collect the filename and store it in a variable once the workbook opens." this is what I think needs to happen but do not know the correct coding. can you give an example. thanks in advance.
 

KniteMare

Board Regular
Joined
Mar 4, 2002
Messages
238
Or try:

Dim NewFileName as String
Sub TestSaveAs()
NewFileName = ActiveWorkBook.Name
ThisWorkbook.SaveAs Filename:=NewFileName, FileFormat:=xlWorkbookNormal
End Sub


Yours in EXCELent Frustration

KniteMare
 

Forum statistics

Threads
1,147,674
Messages
5,742,541
Members
423,736
Latest member
dracula cyrus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top