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: