VBA to Extract Worksheets from a single workbook putting each in its own workbook

BGDunbar

Board Regular
Joined
Jul 26, 2016
Messages
79
I have an Excel workbook with approximately 20 worksheets in it. I want each worksheet to have it's own workbook named as the worksheet tab is named (tab name "District 01", file name "District 01") in same folder. I want to keep the original workbook intact as well so these would be copies of worksheets. I do this on a monthly basis so would like to automate. Is there a way to automate with VBA?

All help is appreciated.
Thank you,
Betty
 
Last edited:

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 this:
Will save each Sheet in workbook into separate workbooks saving Workbook as sheet name in the active workbook Path.

Code:
Sub Add_Workbook()
Application.ScreenUpdating = False
Dim ans As String
ans = ActiveWorkbook.Name
Dim FileName As String
Dim FilePath As String
Dim i As Long
FilePath = ThisWorkbook.Path
For i = 1 To Sheets.Count
    FileName = Sheets(i).Name
    Application.Workbooks.Add
    ActiveWorkbook.SaveAs FileName:=FilePath & "\" & FileName, FileFormat:=52
    Workbooks(ans).Sheets(i).Copy After:=Sheets(Sheets.Count)
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets(1).Delete
    Application.DisplayAlerts = True
    ActiveWorkbook.Save
    ActiveWorkbook.Close
Next
Application.ScreenUpdating = True
MsgBox "All Done"
End Sub
 
Upvote 0
Thank you My Answer Is This. Everything works except the file path for saving the individual workbooks. I am on a network so I'm not sure if that makes a difference. The file is on the network but the individual workbooks wound up on my hard drive which I rarely use. I discovered this when I tried to run the code a second time and got the message about there already being a file there with that name and gave me the path to find them.

If you can get it to save to the network it would be great. If not, I can work with it this way.

Thank you for your quick response.
Betty
 
Upvote 0
Hia
Give this a go
Code:
Sub SplitWb()

    Dim ws As Worksheet
    Dim Pth As String
    
Application.ScreenUpdating = False

    Pth = ActiveWorkbook.Path & "\"
    
    For Each ws In Worksheets
        ws.Copy
        ActiveWorkbook.SaveAs Pth & ws.Name, FileFormat:=51
        ActiveWorkbook.Close
    Next ws

End Sub
 
Upvote 0
I would need to know the Proper path.
Thank you My Answer Is This. Everything works except the file path for saving the individual workbooks. I am on a network so I'm not sure if that makes a difference. The file is on the network but the individual workbooks wound up on my hard drive which I rarely use. I discovered this when I tried to run the code a second time and got the message about there already being a file there with that name and gave me the path to find them.

If you can get it to save to the network it would be great. If not, I can work with it this way.

Thank you for your quick response.
Betty
 
Upvote 0
Glad we could help & thanks for the feedback

Not quit sure why M.A.I.T's code didn't work correctly, as it worked fine for me.
 
Last edited:
Upvote 0
My script would work on your own computer you said but not on the Network. You never said anything in your original post about wanting files saved on a Network.
Thank you Fluff. It works wonderfully. I appreciate your help also My Answer Is This...

:)
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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