Help with macro

chobanne

Active Member
Joined
Jul 3, 2011
Messages
269
Hello all i have a macro like shown bellow. Can someone please add me the lines of code that would somehow remember the original file name, close the renamed file after renaming, and reopen the original file. It would be even better if it is possible to save file as new one without closing and reopening the original one. Thank you

Sub Save_as()

Dim i As Variant
Dim fPath As String

i = InputBox("New name", "Save as")

Range("BN2").Value = i

Dim ans As VbMsgBoxResult
ans = MsgBox("Add more description", vbQuestion + vbYesNo)
If ans = vbYes Then
fPath = ThisWorkbook.Path & Application.PathSeparator
ThisFile = fPath & Range("BO2").Value
ActiveWorkbook.SaveAS Filename:=ThisFile
Else
fPath = ThisWorkbook.Path & Application.PathSeparator
ThisFile = fPath & Range("BP2").Value
ActiveWorkbook.SaveAS Filename:=ThisFile
End If

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi
try using SaveCopyAs method which will save a copy of the workbook to a file but does not modify the open workbook in memory

change this

VBA Code:
ActiveWorkbook.SaveAS Filename:=ThisFile

to this

Rich (BB code):
ActiveWorkbook.SaveCopyAs Filename:=ThisFile

Dave
 
Upvote 0
Solution
Thank you, that's works nice, but can you help me in a way to make macro to do like this. To remember the original file name, close the renamed file , and reopen the original file after that. Because i would like to add some lines of code before closing the renamed file. That changes will only been seen in renamed file.
 
Upvote 0
Thank you, that's works nice, but can you help me in a way to make macro to do like this.

To remember the original file name, close the renamed file
SaveCopyAs’ method of workbook object saves a copy of the workbook as a file and does not change the original filename

and reopen the original file after that.

the open workbook is not closed or changed when using the SaveCopyAs method


Dave
 
Upvote 0

Hi chobanne,​

Try this code:
It will make the copy of your existing file in same folder
VBA Code:
Sub Save_as()
    old_name = ThisWorkbook.Path & Application.PathSeparator & ThisWorkbook.Name
    new_file_name = InputBox("New name", "Save as")
    new_name = ThisWorkbook.Path & Application.PathSeparator & new_file_name & ".xlsx"
    With CreateObject("Scripting.FileSystemObject")
        Call .copyFile(old_name, new_name)
    End With
End Sub
 
Upvote 0
RustamMika i understand your thoughts, but because i added some line in your macro i got new file with changes. The file with changes stays open, but the original file is just saved.
Actually i need to be different. The original file must stay opened and the file with changes to be just saved in same directory. Can you change the code somehow.
 
Upvote 0
Or maybe to tell my where to put my new lines of code, to take a effect on file that will be just saved
 
Upvote 0
It looks you may have worked out that Dave's @dmt32 code will work for.
If you need more information and some sample code Bill Jelen covers it here.
Note: you can't change the file type (extension) using savecopyas which Bill addresses in his code.

 
Upvote 0
@Alex Blakenburg - Interesting article thanks for sharing.
I shared a solution Here I created some while ago for others with similar requirement but not fully sure if it resolved their issues as had no feedback.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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