Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other Microsoft Excel Posts   Ask New Question  Ask New Question With Power Editor

Excel ? Separate rows on a workbook into separate workbooks.
Charles Simpson posted at Tuesday, November 03, 2009 4:28 PM

I need help separating 45k rows of data, in a single worksheet into 45 separate workbooks (not sheets) in CSV format.  (1000 per workbook)

Data is:

First Name   Last Name    Address   Phone   City   State   Email   Notes

 

Any help would be greatly appreciated!

FYI - I am not technical - I am using Excel 2007

Reply    Reply Using Power Editor
Not technical
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0
RE: Excel ? Separate rows on a workbook into separate workbooks.
Jonathan VH provided a rated reply to Charles Simpson on Tuesday, November 03, 2009 5:39 PM

I think you'd have to use a macro... (By the way, a csv file isn't actually an Excel workbook.)

Press Alt+F11 to open the VBA editor and then Ctrl+R to see the Project Explorer. The Project Explorer should show a VBAProject for your workbook. If there is a plus to the left of the VBAProject, cick on the plus (+) to expand the object. If there is no section called Modules, right-click on the VBAProject object and Insert|Module. If there is already a module in the list, double-click on it. You should now have opened an edit window. Copy this code into the window, under any other code that may be there:

Sub MakeCSVs()
  Dim n As Integer, m As Integer, lastRow As Integer, lastCol As Integer
  Dim newWB As Workbook
  Dim path As String, fname As String
  path = "C:\Documents and Settings\Charles\My Documents\"
  filename = "File"
  lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  lastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
  Application.ScreenUpdating = False
  n = 0
  While n * 1000 < lastRow
    Set newWB = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Activate
    Range(Cells(n * 1000 + 1, 1), Cells((n + 1) * 1000, lastCol)).Copy newWB.Sheets(1).Cells(1, 1)
    n = n + 1
    newWB.SaveAs FileName:=path & fname & Format(n, "00") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    newWB.Close False
  Wend
  Application.ScreenUpdating = True
End Sub

You'll need to change the path and fname strings to reflect the path where you want the files saved and the name you want to give each file. The name will have a number appended to it, so, using the above "File" as fname, the first file will be named File01.csv, the second named File02.csv, etc.

Close the VBA editor. In your workbook, press Alt+F8 and double-click on MakeCSVs.

 

Reply    Reply Using Power Editor
  Rank Winnings Points
November 2 $212.00 548
October 2 $171.00 584

RE: oops
Jonathan VH replied to Jonathan VH on Tuesday, November 03, 2009 5:50 PM

A couple of mistakes crept in there. Use this code:

Sub MakeCSVs()
  Dim n As Integer, lastRow As Integer, lastCol As Integer
  Dim newWB As Workbook
  Dim path As String, fname As String
  path = "C:\Documents and Settings\Charles\My Documents\"
  fname = "File"
  lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  lastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
  Application.ScreenUpdating = False
  n = 0
  While n * 1000 < lastRow
    Set newWB = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Activate
    Range(Cells(n * 1000 + 1, 1), Cells((n + 1) * 1000, lastCol)).Copy newWB.Sheets(1).Cells(1, 1)
    n = n + 1
    newWB.SaveAs FileName:=path & fname & Format(n, "00") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    newWB.Close False
  Wend
  Application.ScreenUpdating = True
End Sub

Reply    Reply Using Power Editor
  Rank Winnings Points
November 2 $212.00 548
October 2 $171.00 584

Thank you
Charles Simpson replied to Jonathan VH on Tuesday, November 03, 2009 8:20 PM

I believe I followed your instructions, but I got an error that just said "overflow"

Any ideas??

 

Thanks

Reply    Reply Using Power Editor
Not technical
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

change
Charles Simpson replied to Jonathan VH on Tuesday, November 03, 2009 8:22 PM

I wrote that I had 45k rows, but I really have 40833 rows... Don't know if that makes a difference or not...
Reply    Reply Using Power Editor
Not technical
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

screen shot of error - debug
Charles Simpson replied to Jonathan VH on Tuesday, November 03, 2009 8:32 PM

Sub MakeCSVs()
  Dim n As Integer, lastRow As Integer, lastCol As Integer
  Dim newWB As Workbook
  Dim path As String, fname As String
  path = "C:\Users\Charles Work\4 Staffing\Candidates from Deorker\Candidates to import\"
  fname = "File"
  lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  lastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
  Application.ScreenUpdating = False
  n = 0
  While n * 1000 < lastRow
    Set newWB = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Activate
    Range(Cells(n * 1000 + 1, 1), Cells((n + 1) * 1000, lastCol)).Copy newWB.Sheets(1).Cells(1, 1)
    n = n + 1
    newWB.SaveAs Filename:=path & fname & Format(n, "00") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    newWB.Close False
  Wend
  Application.ScreenUpdating = True
End Sub

 

Reply    Reply Using Power Editor
Not technical
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

RE: Overflow
Jonathan VH provided a rated reply to Charles Simpson on Wednesday, November 04, 2009 6:32 AM

Stupid mistake on my part. A VB integer is signed and only two bytes, so it overflows with more than 32,767 rows. Please try this:

Sub MakeCSVs()
  Dim n As Integer, lastRow As Long, lastCol As Integer
  Dim newWB As Workbook
  Dim path As String, fname As String
  path = "C:\Users\Charles Work\4 Staffing\Candidates from Deorker\Candidates to import\"
  fname = "File"
  lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  lastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
  Application.ScreenUpdating = False
  n = 0
  While n * 1000 < lastRow
    Set newWB = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Activate
    Range(Cells(n * 1000 + 1, 1), Cells((n + 1) * 1000, lastCol)).Copy newWB.Sheets(1).Cells(1, 1)
    n = n + 1
    newWB.SaveAs FileName:=path & fname & Format(n, "00") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    newWB.Close False
  Wend
  Application.ScreenUpdating = True
End Sub

Reply    Reply Using Power Editor
  Rank Winnings Points
November 2 $212.00 548
October 2 $171.00 584

I got another bug...
Charles Simpson replied to Jonathan VH on Wednesday, November 04, 2009 8:14 AM

Sub MakeCSVs()
  Dim n As Integer, lastRow As Long, lastCol As Integer
  Dim newWB As Workbook
  Dim path As String, fname As String
  path = "C:\Users\Charles Work\4 Staffing\Candidates from Deorker\Candidates to import\"
  fname = "File"
  lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  lastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
  Application.ScreenUpdating = False
  n = 0
  While n * 1000 < lastRow
    Set newWB = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Activate
    Range(Cells(n * 1000 + 1, 1), Cells((n + 1) * 1000, lastCol)).Copy newWB.Sheets(1).Cells(1, 1)
    n = n + 1
    newWB.SaveAs Filename:=path & fname & Format(n, "00") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    newWB.Close False
  Wend
  Application.ScreenUpdating = True
End Sub

 

Reply    Reply Using Power Editor
Not technical
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

RE: I got another bug...
Jonathan VH replied to Charles Simpson on Wednesday, November 04, 2009 9:11 AM

What is the error message?
Reply    Reply Using Power Editor
  Rank Winnings Points
November 2 $212.00 548
October 2 $171.00 584

I fixed it... It was a naming issue "file"
Charles Simpson replied to Jonathan VH on Wednesday, November 04, 2009 9:22 AM

I ran into another overflow issue, so I separated into two files, with the first one containing 30k rows and the second the remaining rows.  When I ran the same code on the second file I got a naming issue with using "file" again. I just changed the name to "file3" and it worked great. 

I do have another request though. I have columns labels on the top of the entire file and I would like them to be on each individual sheet, your code created.  If this is too much trouble I can just copy and paste each page. 

 

Thanks for all your help!

Reply    Reply Using Power Editor
Not technical
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

RE: I fixed it... It was a naming issue "file"
Jonathan VH provided a rated reply to Charles Simpson on Wednesday, November 04, 2009 9:52 AM

If you don't mind the first file having 1000 rows and the subsequent files having 1001 rows, you could try this:

Sub MakeCSVs()
  Dim n As Long, lastRow As Integer, lastCol As Integer
  Dim newWB As Workbook
  Dim path As String, fname As String
  path = "C:\Users\Charles Work\4 Staffing\Candidates from Deorker\Candidates to import\"
  fname = "File"
  lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  lastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
  Application.ScreenUpdating = False
  n = 0
  While n * 1000 < lastRow
    Set newWB = Workbooks.Add(xlWBATWorksheet)
    ThisWorkbook.Activate
    If n = 0 Then
      Range(Cells(n * 1000 + 1, 1), Cells((n + 1) * 1000, lastCol)).Copy newWB.Sheets(1).Cells(1, 1)
    Else
      Range(Cells(1, 1), Cells(1, lastCol)).Copy newWB.Sheets(1).Cells(1, 1)
      Range(Cells(n * 1000 + 1, 1), Cells((n + 1) * 1000, lastCol)).Copy newWB.Sheets(1).Cells(2, 1)
    End If
    n = n + 1
    newWB.SaveAs FileName:=path & fname & Format(n, "00") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
    newWB.Close False
  Wend
  Application.ScreenUpdating = True
End Sub

That should give you the same result as manually inserting the header row.

The "naming" issue isn't a bug; that was intentional. It won't let you overwrite existing files with the same names. I fixed the other overflow problem, so you shouldn't need to split the workbook manually.

Reply    Reply Using Power Editor
  Rank Winnings Points
November 2 $212.00 548
October 2 $171.00 584

All set! Thank you very much!
Charles Simpson replied to Jonathan VH on Wednesday, November 04, 2009 10:42 AM

end of post
Reply    Reply Using Power Editor
Not technical
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0