Microsoft Excel - Excel ? Separate rows on a workbook into separate workbooks.

Asked By Charles Simpson
03-Nov-09 04: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

  Jonathan VH replied to Charles Simpson
03-Nov-09 05: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.

 

  Jonathan VH replied to Jonathan VH
03-Nov-09 05: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

Thank you  Thank you

03-Nov-09 08:20 PM

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

Any ideas??

 

Thanks

change  change
03-Nov-09 08: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...
screen shot of error - debug  screen shot of error - debug
03-Nov-09 08: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

 

  Jonathan VH replied to Charles Simpson
04-Nov-09 06: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

I got another bug...  I got another bug...
04-Nov-09 08: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

 

  Jonathan VH replied to Charles Simpson
04-Nov-09 09:11 AM
What is the error message?
I fixed it... It was a naming issue   I fixed it... It was a naming issue "file"
04-Nov-09 09: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!

  Jonathan VH replied to Charles Simpson
04-Nov-09 09: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.

All set!  Thank you very much!  All set! Thank you very much!
04-Nov-09 10:42 AM
end of post
Create New Account
help
EXCEL SHEETS Excel Can you reinstate deleted sheets in excel, if so how please. thanks Excel Worksheet Discussions Microsoft Excel (1) Worksheet (1) Workbook (1) Daisy (1) Closeit (1) If you haven't saved the
Non synchronisation in split screens in Microsoft Excel Excel Lotus 123 has the ability to split a worksheet in two, similar to Microsoft Excel, but in Lotus 123 you have the ability to scroll down one side whilst the other side remains stationary. Can you do that in Microsoft Excel and if so how? Excel Miscellaneous Discussions Microsoft Excel (1) Worksheet (1) Workbook (1) VB (1) Normek (1) Lotus (1
How can I make my data show as flashing in Excel? Excel Excel Miscellaneous Discussions Microsoft Excel (1) Excel (1) Application.OnTime (1) Worksheet (1) Workbook (1) Macro (1) VBA (1) Font.ColorIndex (1 if you insist, see Chip Pearson's site for VBA code. http: / / www.cpearson.com / excel / BlinkingText.aspx Gord Dibben MS Excel MVP This was great - thank you. My boss loved it. Now I have another question
No colours on excel sheets Excel Excel 2003 No colours show up if i select a colour for figures, letters, frames etc. When printing all colours appear correctly. Which button I pressed by mistake? Excel Crashes GPFs Discussions Excel (1) Windows (1) If this is a problem with all Excel files with coloured cells, the Windows OS high contrast setting may be turned on. There MSKB article: OFF: Changes to Fill Color and Fill Pattern Are Not Displayed http: / / support.microsoft.com / default.aspx?scid = kb;en-ca;320531 - - Gary''s Student - gsnu200715 keywords: No, colours
Opening and retrieving information from numerous excel sheets Excel I am currently trying to do work with numerous different excel sheets. I am looking for the user to open a blank Excel worksheet and click a button. This button will make a dialogue box appear and ask a cell from each sheet opened (for example cell A1). How would I do this? Excel Programming Discussions SQL Server 2000 (1) Office 2003 (1) Office (1) Excel (1) Worksheet (1) VbMaximizedFocus (1) BZeyger (1) Rogers (1) Here's something to get you