Save File as the "FileSelectedtoOpenInMacro(Extract)"

thumperjlp

New Member
Joined
Jul 27, 2012
Messages
2
When run, my macro prompts the user to select a file to copy, which it then copies the specified sheet & pastes to a new sheet. The selected file will then close and the macro then formats, deletes, etc. the data in the copied sheet.
I then want to save this new formatted file as the name of the file which the user selected at the beginning of the macro and add "extract" to the end of the file name. i.e. userselectedfile(extract).xls so as not to overwrite the original file.
I've searched high and low for a solution with no luck. Any help would be greatly appreciated.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi and Welcome to the Board,

Below is some code you could try.

Code:
Sub AppendName_SaveAs()
    Dim sNewName As String, sExt As String
    Const sAppend As String = "(extract)"

    With ActiveWorkbook
        sExt = "." & Split(.Name, ".")(UBound(Split(.Name, ".")))
        sNewName = Left(.Name, Len(.Name) - Len(sExt)) _
            & sAppend & sExt
        .SaveAs Filename:=.Path & "\" & sNewName
    End With

End Sub
 
Upvote 0
Hi Jerry,

Nice Script. Another alternative script which includes File naming like an Accountant lol.


Code:
Sub AppendName_SaveAsBKFix()
    Dim sNewName As String, sExt As String
    Const sAppend As String = "(extract)"
    With ActiveWorkbook
        sExt = "." & Split(.Name, ".")(UBound(Split(.Name, ".")))
        sNewName = Left(.Name, Len(.Name) - Len(sExt)) _
            & sAppend & " " & "as at " & Format(Now, "dd-mmm-yy h-mm-ss") & sExt
        .SaveAs Filename:=.Path & "\" & sNewName
    End With
End Sub

Biz
 
Upvote 0
Great; both worked perfectly. Is there a way to delete the macros in the new(copied) sheet before saving it so the user isn't prompted to save work when trying to close the extract? It seems as though this wouldn't be possible because the macro is needed to save the file, but if anyone has a solution that would be awesome.
 
Upvote 0
Great; both worked perfectly. Is there a way to delete the macros in the new(copied) sheet before saving it so the user isn't prompted to save work when trying to close the extract? It seems as though this wouldn't be possible because the macro is needed to save the file, but if anyone has a solution that would be awesome.

If you are using xl2007 or later, you can save to an .xlsx format. This doesn't immediately delete the macro, but if you close then reopen the file the macros will be gone.

Code:
Sub AppendName_SaveAsBKFix()
    Dim sNewName As String, sExt As String
    Const sAppend As String = "(extract)"
    With ActiveWorkbook
        sExt = "." & Split(.Name, ".")(UBound(Split(.Name, ".")))
        sNewName = Left(.Name, Len(.Name) - Len(sExt)) _
            & sAppend & " " & "as at " & Format(Now, "dd-mmm-yy h-mm-ss") & ".xlsx"
        Application.DisplayAlerts = False 'Optional -Hide alert that macros won't be saved.
        .SaveAs Filename:=.Path & "\" & sNewName, FileFormat:=xlOpenXMLWorkbook
        Application.DisplayAlerts = True
    End With
End Sub

Altenatively, if this is a function your users do often, you might want to place it in their Personal.xlsb file or in an add-in file.
 
Upvote 0

Forum statistics

Threads
1,215,601
Messages
6,125,763
Members
449,259
Latest member
rehanahmadawan

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