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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
Or try:

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


Yours in EXCELent Frustration

KniteMare
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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
Back
Top