Can I split a spreadsheet into multiple files based on a column in Excel 2007?
For posterity, here's yet another macro to tackle this problem.
This macro will go through a specified column, top down, and split to a new file whenever a new value is encountered. Blanks or repeated values are kept together (as well as total rows), but your column values must be sorted or unique. I primarily designed it to work with PivotTables layout (once converted to values).
So as it is, there's no need to modify the code or prepare a named range. The macro starts by prompting the user for the column to process, as well as the row number at which to start - that is to skip the headers, and goes from there.
When a section is identified, rather than copying those values to another sheet, the entire worksheet is copied to a new workbook and all rows below and above the section are deleted. This allows to keep any printing setup, conditional formatting, charts or whatever else you might have in there, as well as keeping the header in each split file which is useful when distributing these files.
Files are saved in a \Split\ subfolder with the cell value as the filename. I have not yet extensively tested it on a variety of documents, but it works on my sample files. Feel free to try it out and let me know if you have issues.
The macro can be saved as an excel add-in (xlam) to add a button on the ribbon/quickaccess toolbar button for easy access.
Public Sub SplitToFiles()
' MACRO SplitToFiles
' Last update: 2019-05-28
' Author: mtone
' Version 1.2
' Description:
' Loops through a specified column, and split each distinct values into a separate file by making a copy and deleting rows below and above
'
' Note: Values in the column should be unique or sorted.
'
' The following cells are ignored when delimiting sections:
' - blank cells, or containing spaces only
' - same value repeated
' - cells containing "total"
'
' Files are saved in a "Split" subfolder from the location of the source workbook, and named after the section name.
Dim osh As Worksheet ' Original sheet
Dim iRow As Long ' Cursors
Dim iCol As Long
Dim iFirstRow As Long ' Constant
Dim iTotalRows As Long ' Constant
Dim iStartRow As Long ' Section delimiters
Dim iStopRow As Long
Dim sSectionName As String ' Section name (and filename)
Dim rCell As Range ' current cell
Dim owb As Workbook ' Original workbook
Dim sFilePath As String ' Constant
Dim iCount As Integer ' # of documents created
iCol = Application.InputBox("Enter the column number used for splitting", "Select column", 2, , , , , 1)
iRow = Application.InputBox("Enter the starting row number (to skip header)", "Select row", 2, , , , , 1)
iFirstRow = iRow
Set osh = Application.ActiveSheet
Set owb = Application.ActiveWorkbook
iTotalRows = osh.UsedRange.Rows.Count
sFilePath = Application.ActiveWorkbook.Path
If Dir(sFilePath + "\Split", vbDirectory) = "" Then
MkDir sFilePath + "\Split"
End If
'Turn Off Screen Updating Events
Application.EnableEvents = False
Application.ScreenUpdating = False
Do
' Get cell at cursor
Set rCell = osh.Cells(iRow, iCol)
sCell = Replace(rCell.Text, " ", "")
If sCell = "" Or (rCell.Text = sSectionName And iStartRow <> 0) Or InStr(1, rCell.Text, "total", vbTextCompare) <> 0 Then
' Skip condition met
Else
' Found new section
If iStartRow = 0 Then
' StartRow delimiter not set, meaning beginning a new section
sSectionName = rCell.Text
iStartRow = iRow
Else
' StartRow delimiter set, meaning we reached the end of a section
iStopRow = iRow - 1
' Pass variables to a separate sub to create and save the new worksheet
CopySheet osh, iFirstRow, iStartRow, iStopRow, iTotalRows, sFilePath, sSectionName, owb.fileFormat
iCount = iCount + 1
' Reset section delimiters
iStartRow = 0
iStopRow = 0
' Ready to continue loop
iRow = iRow - 1
End If
End If
' Continue until last row is reached
If iRow < iTotalRows Then
iRow = iRow + 1
Else
' Finished. Save the last section
iStopRow = iRow
CopySheet osh, iFirstRow, iStartRow, iStopRow, iTotalRows, sFilePath, sSectionName, owb.fileFormat
iCount = iCount + 1
' Exit
Exit Do
End If
Loop
'Turn On Screen Updating Events
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox Str(iCount) + " documents saved in " + sFilePath
End Sub
Public Sub DeleteRows(targetSheet As Worksheet, RowFrom As Long, RowTo As Long)
Dim rngRange As Range
Set rngRange = Range(targetSheet.Cells(RowFrom, 1), targetSheet.Cells(RowTo, 1)).EntireRow
rngRange.Select
rngRange.Delete
End Sub
Public Sub CopySheet(osh As Worksheet, iFirstRow As Long, iStartRow As Long, iStopRow As Long, iTotalRows As Long, sFilePath As String, sSectionName As String, fileFormat As XlFileFormat)
Dim ash As Worksheet ' Copied sheet
Dim awb As Workbook ' New workbook
' Copy book
osh.Copy
Set ash = Application.ActiveSheet
' Delete Rows after section
If iTotalRows > iStopRow Then
DeleteRows ash, iStopRow + 1, iTotalRows
End If
' Delete Rows before section
If iStartRow > iFirstRow Then
DeleteRows ash, iFirstRow, iStartRow - 1
End If
' Select left-topmost cell
ash.Cells(1, 1).Select
' Clean up a few characters to prevent invalid filename
sSectionName = Replace(sSectionName, "/", " ")
sSectionName = Replace(sSectionName, "\", " ")
sSectionName = Replace(sSectionName, ":", " ")
sSectionName = Replace(sSectionName, "=", " ")
sSectionName = Replace(sSectionName, "*", " ")
sSectionName = Replace(sSectionName, ".", " ")
sSectionName = Replace(sSectionName, "?", " ")
sSectionName = Strings.Trim(sSectionName)
' Save in same format as original workbook
ash.SaveAs sFilePath + "\Split\" + sSectionName, fileFormat
' Close
Set awb = ash.Parent
awb.Close SaveChanges:=False
End Sub
As far as I know there is nothing short of a macro that going to split you data and automatically save it onto a set of files for you. VBA is probably easier.
Update I implemented my suggestion. It loops through all the names defined in the named range 'RepList'. The named range is a dynamic named range of the form =OFFSET(Names!$A$2,0,0,COUNTA(Names!$A:$A)-1,1)
module follows.
Option Explicit
'Split sales data into separate columns baed on the names defined in
'a Sales Rep List on the 'Names' sheet.
Sub SplitSalesData()
Dim wb As Workbook
Dim p As Range
Application.ScreenUpdating = False
For Each p In Sheets("Names").Range("RepList")
Workbooks.Add
Set wb = ActiveWorkbook
ThisWorkbook.Activate
WritePersonToWorkbook wb, p.Value
wb.SaveAs ThisWorkbook.Path & "\salesdata_" & p.Value
wb.Close
Next p
Application.ScreenUpdating = True
Set wb = Nothing
End Sub
'Writes all the sales data rows belonging to a Person
'to the first sheet in the named SalesWB.
Sub WritePersonToWorkbook(ByVal SalesWB As Workbook, _
ByVal Person As String)
Dim rw As Range
Dim personRows As Range 'Stores all of the rows found
'containing Person in column 1
For Each rw In UsedRange.Rows
If Person = rw.Cells(1, 1) Then
If personRows Is Nothing Then
Set personRows = rw
Else
Set personRows = Union(personRows, rw)
End If
End If
Next rw
personRows.Copy SalesWB.Sheets(1).Cells(1, 1)
Ser personRows = Nothing
End Sub
This workbook contains the code and the named range. The code is part of the 'Sales Data' sheet.
If someone else answers with the correct way of doing this that is quick, please ignore this answer.
I personally find myself using Excel and then spending a lot of time (somtimes hours) looking for a complicated way to do something or an over the top equation that will do everything when I will never use it again... and it turns out that if I just sat down and got on with the task manually it would take a fraction of the time.
If you only have a handful of people, what I recommend you do is simply highlight all the data, go to the data tab and click the sort button.
You can then choose what column to sort by, in your case you want to use Repname, then just copy and paste to individual files.
I am sure that using VBA or other tools, you may come up with a solution but the fact is, you will be looking at hours upon hours of work when just getting on with it by using the above method should get you done in next to no time.
Also, I think you can do this sort of thing on sharepoint + excel services, but that is a way over the top solution for this sort of thing.