Save & close source file, and Save & keep Destination file open

Webbers

New Member
Joined
Oct 20, 2004
Messages
41
I have functional macro, but I simply cannot figure out one part. I want the "template", which is where the macro is stored to save AND close at the end (with no prompts), and I want the "destination" workbook (where copies of worksheets were placed) to save AND remain open. There is a section of code at the bottom that I thought would do this, but it does not. What happens is that BOTH workbooks close and I am left with Excel open but no workbooks. The user will need to work with the saved file ("destination"), but the template will not be needed after the new file is created.

Code:
Sub Save_Seperate_Sheets()

' Saves multiple sheets in another workbook based on the cell values
Dim fName As String         ' Output File Name
Dim Path1 As String         ' Path name (current directory)
Dim xlD As Workbook         ' Output file
Dim xlS As Workbook         ' THIS workbook
Dim shS As Worksheet        ' Worksheets in current workbook

' Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual

Path1 = ThisWorkbook.Path
fName = Sheets("Main").Range("$C$4") & " " & Format(Sheets("Main").Range("$I$1"), "mm.dd.yy") & ".xlsx"
Set xlS = ThisWorkbook

' Rename sheets
    Call RenameSheets
    
' Create the new workbook
Set xlD = Workbooks.Add

' Copy sheets in
For Each shS In xlS.Sheets
    If shS.Name <> "Main" Then
        shS.Copy after:=xlD.Sheets(Sheets.Count)
        xlD.Sheets(Sheets.Count).Name = shS.Name
    End If
Next shS

' Remove the superfluous sheets
xlD.Sheets(1).Delete ' <!-- Removes sheet1 (Main)

' Hide the code sheet
xlD.Sheets("codes").Visible = xlHidden
     
' Save the workbook
xlD.SaveAs FileName:=Path1 & "\" & fName, FileFormat:=51

'Close the workbook
xlD.Close False ' <!---  ' Output file
xlS.Close True ' <!--- ' THIS workbook

ResetSettings:
' Reset Macro Optimization Settings
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
   
End Sub
 
Last edited by a moderator:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try
Code:
'Close the workbook
xlD.Save

ResetSettings:
' Reset Macro Optimization Settings
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
   xlS.Close True ' <!--- ' THIS workbook

End Sub
 
Last edited:
Upvote 0
Fluff-----

Would you be surprised if I told you it worked perfectly? I didn't think so. I went back and reviewed the template (closed file) and that saved properly too. Thanks so much!!!!
 
Upvote 0
You're welcome & thanks for the feedback.

One thing to remember.
If you have disabled things like events, calculation etc, you need to re-enable them before closing the workbook that is running the code.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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