If you use Excel quite a bit, you know you may get some rather large workbooks from colleagues. Often it is desirable to break the workbook down, so that each worksheet is in its own workbook. While this can be done manually, the process quickly becomes tedious if you have a lot of breaking down to do.
This sort of repetitive work is a natural for a macro. The following macro, called BreakItUp, creates individual workbook files based on the worksheets in the current workbook. Thus, if the current workbook contains 25 worksheets, running this macro results in 25 individual Excel workbook files being created. Each workbook has a single worksheet, and the name of the workbook is the same as that of the worksheet.
Sub BreakItUp() Dim sht As Worksheet Dim NFName As String Const WBPath = "C:\" For Each sht In ActiveWorkbook.Worksheets sht.Copy NFName = WBPath & sht.Name & ".xls" ActiveWorkbook.SaveAs FileName:=NFName, _ FileFormat:=xlNormal, CreateBackup:=False ActiveWindow.Close Next End Sub
The BreakItUp macro stores the new workbooks in the root directory on the C: drive. If you want your workbooks saved in a different place, you can simply change the line in which the WBPath constant is created.
You should also know that it is relatively easy to crash this macro. For instance, if you use a character in a worksheet name that is not “legal” for a file name, the macro will rudely stop when it tries to create the file. Of course, you could easily make the modifications to the macro to check for and replace such illegal characters.
Another potential pitfall for the macro is that it will stop running if a file already exists that has the same name as a worksheet. For instance, let's suppose you have a worksheet named MySheet1. If there is already a file on disk called MySheet1.xls, then the macro will stop when it tries to overwrite the file. You can get around this by making sure there are no file name conflicts in the directory where the workbooks are being saved.
Finally, if your original workbook has formulas that reference other worksheets (besides the one on which the formula occurs), then those formulas won't work in the broken-out workbooks. For this reason, you'll want to carefully check what is created to make sure the workbooks fit your needs.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12273) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Creating Individual Workbooks.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Need to allow others to contribute to your Excel workbook? It's easy to do if you just share it. This tip provides an ...
Discover MoreWant Excel to remember where your workbooks were located on the screen and then open them in the same position the next ...
Discover MoreIf you are afraid of messing up a workbook, consider doing your work on a clone of the workbook. Excel provides an easy ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-03-27 10:51:58
Dave Bonin
As useful as this macro might be, I've often wondered whether a better approach would be to make X copies of the workbook where X is the number of worksheets. Each workbook copy would then remove all worksheets except the one which is supposed to remain.
Why do this? To preserve the macros and special formatting that might otherwise be lost.
2017-03-27 03:05:59
Hi Alex
Thanks, I have it working now. ( I had to Dim a couple of variables ( nmUseAddrs As String , i As Long ) as I use Option Explicit ).
Alan
2017-03-26 19:34:33
Alex B
Hello Alan, I tend to reuse a lot of code and stick it in a Library module, so I forgot that I called the code below. (Your point 2). Sorry about the omission.
PS: I use selected sheets in my main macro rather than all sheets, it makes it easier when testing if nothing else.
Function FindAddress(FindString, Optional Whole_Part)
' Find a supplied text in the active worksheet and return the address information.
If Whole_Part = "" Then Whole_Part = xlWhole
On Error GoTo ErrorHandling
FindAddress = Cells.Find(What:=FindString, After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=Whole_Part, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Address
Exit Function:
ErrorHandling:
FindAddress = ""
End Function
2017-03-26 04:50:03
Hi Alex B,
Thanks for sharing. I tried your codes..
( _1) End Function was missing on your first Function )
_2) I error at
FindAddress(
_...........
This is the code I tried. ( I am using XL 2007 and saving as .xlsm )
' https://excelribbon.tips.net/T012273_Creating_Individual_Workbooks.html
Sub BreakItUp()
Dim Ws As Worksheet
Dim NFName As String, WBPath As String
Let WBPath = ThisWorkbook.Path & "\" ' "C:\"
For Each Ws In ActiveWorkbook.Worksheets
Ws.Copy ' This effectively makes a copy File with one Worksheet in it, Ws
' NFName = WBPath & Ws.Name & ".xls"
' ActiveWorkbook.SaveAs Filename:=NFName, FileFormat:=xlNormal, CreateBackup:=False
Let NFName = WBPath & Ws.Name & ".xlsm"
ActiveWorkbook.SaveAs Filename:=NFName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Call DeleteBadRefs_and_Links(ActiveWorkbook)
Call BreakLinks(ActiveWorkbook)
ActiveWindow.Close
Next
End Sub
Function DeleteBadRefs_and_Links(wkb As Workbook)
Dim nm As Name
For Each nm In wkb.Names
If InStr(1, nm.RefersTo, "#REF!") > 0 Or InStr(1, nm.RefersTo, "[") > 0 Then
Let nmUseAddrs = FindAddress(FindString:=nm.Name, Whole_Part:=xlPart)
If nmUseAddrs <> "" Then
With wkb.Sheets(1).Range(nmUseAddrs)
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
End If
nm.Delete
End If
Next nm
End Function
Function BreakLinks(wkb As Workbook)
Dim Links As Variant
Links = wkb.LinkSources(Type:=xlLinkTypeExcelLinks)
If Not (IsEmpty(Links)) Then
For i = 1 To UBound(Links)
wkb.BreakLink _
Name:=Links(i), _
Type:=xlLinkTypeExcelLinks
Next i
End If
End Function
2017-03-25 06:58:17
Alex B
There is quite a bit more you could add to this. To start with it would be safer to save the spreadsheet as xlsx so that you at least don't hit the xls row limit issue when you save it.
From my base code I make 2 function calls to:-
- replace range names linked to the original s/sheet with values and then delete the range name
- Break the Links to the original s/sheet replacing it with values.
The functions I am using are below.
Function DeleteBadRefs_and_Links(wkb As Workbook)
Dim nm As Name
For Each nm In wkb.Names
If InStr(1, nm.RefersTo, "#REF!") > 0 _
Or InStr(1, nm.RefersTo, "[") > 0 Then
nmUseAddrs = FindAddress(FindString:=nm.Name, Whole_Part:=xlPart)
If nmUseAddrs <> "" Then
With wkb.Sheets(1).Range(nmUseAddrs)
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
End If
nm.Delete
End If
Next nm
Function BreakLinks(wkb As Workbook)
Dim Links As Variant
Links = wkb.LinkSources(Type:=xlLinkTypeExcelLinks)
If Not (IsEmpty(Links)) Then
For i = 1 To UBound(Links)
wkb.BreakLink _
Name:=Links(i), _
Type:=xlLinkTypeExcelLinks
Next i
End If
End Function
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments