Split Workbook into separate workbooks with a copy of original sheets in each and a sheet with a single condition split

rsur

New Member
Joined
Jan 28, 2023
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I have an excel workbook ("Master") that has multiple worksheets - two worksheets ("Overview" and "Tip Sheet") have info explaining what the workbooks are about, a third ("Datasheet") has the data for all departments, and finally worksheets for each department. I need to split the Master workbook into separate workbooks for each department that I can share with them. I tried to accomplish this using the VBA code -

Sub SplitbyService()
Dim FPath As String
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Sheets
If ws.Name <> "Overview" And ws.Name <> "Tip Sheet" And ws.Name <> "Datasheet" Then
ws.Copy
Application.ActiveWorkbook.SaveAs Filename:=FPath & "\" & ws.Name & ".xlsx"
Application.ActiveWorkbook.Close False
End If

Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

However, these resulting workbooks ("New") all have a single sheet with the a department's data. I want each of the "New" workbooks to also contain both the "Overview" and "Tip Sheet" sheets as separate sheets (So a total of 3 sheets in each "New" workbook). Can someone please help me with this?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this macro:
VBA Code:
Public Sub Create_Department_Workbooks()

    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False 'suppress warning if file already exists
    
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Overview" And ws.Name <> "Tip Sheet" And ws.Name <> "Datasheet" Then
            Worksheets(Array("Overview", "Tip Sheet", ws.Name)).Copy
            ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & ws.Name & ".xlsx", FileFormat:=xlOpenXMLWorkbook
            ActiveWorkbook.Close False
        End If
    Next
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 1
Solution
Thanks John! That worked like a charm. Appreciate your help.
 
Upvote 0
I have a follow-up question. The current code saves the new file name as - ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & ws.Name & ".xlsx", FileFormat:=xlOpenXMLWorkbook.

How can I add current date (Date function) to each files name? Thanks for your help!
 
Upvote 0
I have a follow-up question. The current code saves the new file name as - ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & ws.Name & ".xlsx", FileFormat:=xlOpenXMLWorkbook.

How can I add current date (Date function) to each files name? Thanks for your help!
Done. Was able to do it using Format(Date, "mm.dd.yyyy")
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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