Split Single Worksheet Into Multiple Workbooks

GoHeels

New Member
Joined
May 14, 2008
Messages
27
I preface this with "Thanks" to any assistance provided. Basically, I have a workbook that contains a single worksheet named "Details". Within this sheet is a column named "Location" (Col B), which will be used as my criteria to perform the split. What I would like to do is create a new workbook (located in the same directory as the master file) on each change in value of the "Location" column (Col B).

Thanks to an old post http://www.mrexcel.com/forum/showthr...plit+worksheet by J. Windebank , I was able to locate the following code that almost delivers the results:

Public Sub CreateUserFiles()

Dim DataSheet As Worksheet
Dim UserBook As Workbook
Dim UserSheet As Worksheet
Dim Names As New Collection
Dim NameLoop As Long
Dim UniqueName As Boolean
Dim RowLoop As Long
Dim Folder As String

Application.DisplayAlerts = False

Set DataSheet = ActiveSheet
Folder = "C:\Documents and Settings\rewrde5\Desktop\CA Feed\"

For RowLoop = 1 To DataSheet.Range("B65536").End(xlUp).Row

UniqueName = True

For NameLoop = 1 To Names.Count

If DataSheet.Range("B" & RowLoop) = Names(NameLoop) Then

UniqueName = False
Exit For

End If

Next NameLoop

If UniqueName Then

Names.Add DataSheet.Range("B" & RowLoop)

End If

Next RowLoop

For NameLoop = 1 To Names.Count

Set UserBook = Workbooks.Add
Set UserSheet = UserBook.Worksheets.Add

UserSheet.Name = "Details"
UserBook.Worksheets(Array("Sheet1", "Sheet2", "Sheet3")).Delete

For RowLoop = 1 To DataSheet.Range("B65536").End(xlUp).Row

If DataSheet.Range("B" & RowLoop) = Names(NameLoop) Then

DataSheet.Range("C" & RowLoop & ":IV" & RowLoop).Copy

If IsEmpty(UserSheet.Range("A1")) Then

UserSheet.Range("A1").PasteSpecial

Else

UserSheet.Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial

End If

End If

Next RowLoop

UserBook.SaveAs Folder & Names(NameLoop) & ".xls"
UserBook.Close False

Next NameLoop

Application.DisplayAlerts = True
MsgBox "Completed Processing", vbInformation, "Finished"

End Sub

The only issue is, the new workbooks that are created do not contain column headings. I am not versed enough in VB to determine the code and where it should be inserted to ensure that the headings are copied to the new workbooks.

As previously mentioned, any assitance would be greatly appreciated.

Regards,
Rick
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Great thanks. The code you posted works. However, is it possible to mirror the column width formatting from the master file?
 
Upvote 0
Try:

Code:
Sub Test()
    Const xlColumnWidths = 8
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim c As Range
    Dim List As New Collection
    Dim Item As Variant
    Dim WB As Workbook
    Application.ScreenUpdating = False
''   *** Change Sheet name to suit ***
    Set Sh = Worksheets("Sheet1")
    Set Rng = Sh.Range("A2:A" & Sh.Range("A65536").End(xlUp).Row)
    On Error Resume Next
    For Each c In Rng
        List.Add c.Value, CStr(c.Value)
    Next c
    On Error GoTo 0
    Set Rng = Sh.Range("A1:H" & Sh.Range("A65536").End(xlUp).Row)
    For Each Item In List
        Set WB = Workbooks.Add
        Sh.Cells.Copy
        WB.Worksheets(1).Range("A1").PasteSpecial Paste:=xlColumnWidths, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        Rng.AutoFilter Field:=1, Criteria1:=Item
        Rng.SpecialCells(xlCellTypeVisible).Copy WB.Worksheets(1).Range("A1")
        Rng.AutoFilter
        With WB
            .SaveAs ThisWorkbook.Path & "\" & Item & ".xls"
            .Close
        End With
    Next Item
    Sh.Activate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks, and yes that works. My apologies, however I do have one additional question. Within this file is a "HYPERLINK" formula that is converting a text string to a active hyperlink that routes the user to a image URL once clicked. When the new files are created and the paste function is performed, the hyperlink is not longer hot and requries the user to either (a) click on/off to reactivate or (b) re-insert the formula. Would a paste special resolve this?
 
Upvote 0
I can't reproduce that problem in Excel 2000. If I copy a cell containing a formula that uses the HYPERLINK function to a new workbook, the hyperlink works perfectly.
 
Upvote 0
Strange. When I view the files that are created via the code you provided, the cells containing the URL is no longer formulated, however they are underscored and highlighted in blue as any normal hyperlink. But you are unable to single click the URL link (it is now listed as text). I can double click on a cell containing the URL, then click to an empty cell, and then return to the previously clicked URL cell and the hyperlink now works. Not certain, however I will attempt to incorporate a work around. Thanks for your help!!
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
Latest member
excelknuckles

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