[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Split Excel sheet into small csv files

Posted on 2009-04-27
6
Medium Priority
?
442 Views
Last Modified: 2012-06-27
Hi Everyone
I've just been tasked to create a macro that allows users to split a large excel sheet of around 20,000 rows into small csv files of around 100 rows.  The csv files then need to be saved to a directory. Unfortunately I don't have that much Excel VBA experience.

I would be greatful for any help
Thanks

Andrew
0
Comment
Question by:alines
  • 4
5 Comments
 
LVL 13

Expert Comment

by:Brian Withun
ID: 24244429
Try this.  It creates as many files as is necessary, named like:

OUTPUT_00000-00099.csv
OUTPUT_00100-00199.csv
OUTPUT_00200-00299.csv
...

Where each one contains:

"101303,Emp101303,5/3/1986,3"
"101304,Emp101304,5/11/1955,1"
"101305,Emp101305,5/8/1966,1"
"101306,Emp101306,5/3/1985,3"
...

Option Explicit
 
Const STEPSIZE As Integer = 100
 
Sub ExportChunks()
 
    Dim r As Long ' r = input sheet row
    Dim l As Long ' l = line # of output
    Dim f As Long ' f = field number of record
    Dim s As String ' an output record
    
    Dim fName As String
    Dim DestFile
    
    Let r = 0
    While r <= ActiveSheet.Cells(65535, "A").End(xlUp).Row
    
        Let fName = "C:\OUTPUT_" & Format(r, "00000") & "-" & Format(r + STEPSIZE - 1, "00000") & ".csv"
        
        ' Attempt to open destination file for output.
        Open fName For Output As #1
        
        Let l = 0
        While (l < (r + STEPSIZE - 1)) And (l <= ActiveSheet.Cells(65535, "A").End(xlUp).Row)
        
            Let f = 2
            Let s = ActiveSheet.Cells(r + l + 1, 1).Value
            
            While f <= ActiveSheet.Cells(r + l + 1, 255).End(xlToLeft).Column
            
                s = s & "," & ActiveSheet.Cells(r + l + 1, f).Value
                
                Let f = f + 1
            Wend
            
            Write #1, s
            
            Let l = l + 1
            
        Wend
        
        Close #1
            
        r = r + STEPSIZE
        
    Wend
    
End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:Brian Withun
ID: 24244507
Rather, try this one.  The solution above is quoting all the data in the CSV, which botches the import.

This version uses "Print()" rather than "Write()" and the problem goes away...


Option Explicit
 
Const STEPSIZE As Integer = 100
 
Sub ExportChunks()
    Dim r As Long ' r = input sheet row
    Dim l As Long ' l = line # of output
    Dim f As Long ' f = field number of record
    Dim s As String ' an output record
    Dim fName As String
    Dim DestFile
    Let r = 0
    While r <= ActiveSheet.Cells(65535, "A").End(xlUp).Row
        Let fName = "C:\OUTPUT_" & Format(r, "00000") & "-" & Format(r + STEPSIZE - 1, "00000") & ".csv"
        ' Attempt to open destination file for output.
        Open fName For Output As #1
        Let l = 0
        While (l < (r + STEPSIZE - 1)) And (l <= ActiveSheet.Cells(65535, "A").End(xlUp).Row)
            Let f = 2
            Let s = ActiveSheet.Cells(r + l + 1, 1).Value
            While f <= ActiveSheet.Cells(r + l + 1, 255).End(xlToLeft).Column
                s = s & "," & ActiveSheet.Cells(r + l + 1, f).Value
                Let f = f + 1
            Wend
            Print #1, s
            Let l = l + 1
        Wend
        Close #1
        r = r + STEPSIZE
    Wend
End Sub

Open in new window

0
 

Author Comment

by:alines
ID: 24251464
Hi

Thanks for this.  Definately on the right track.

The problem I'm getting is that the second csv file has actually got two hundred rows going into it, the third 215.  Any ideas ?

Thanks

Andrew
0
 
LVL 13

Accepted Solution

by:
Brian Withun earned 2000 total points
ID: 24256814
Try this one:
Option Explicit
 
Const STEPSIZE As Integer = 100
 
Sub ExportChunks()
    Dim r As Long ' r = input sheet row
    Dim l As Long ' l = line # of output
    Dim f As Long ' f = field number of record
    Dim s As String ' an output record
    Dim fName As String
    Dim DestFile
    Let r = 1
    While r <= ActiveSheet.Cells(65535, "A").End(xlUp).Row
        Let fName = "C:\OUTPUT_" & Format(r, "00000") & "-" & Format(r + STEPSIZE - 1, "00000") & ".csv"
        ' Attempt to open destination file for output.
        Open fName For Output As #1
        Let l = 0
        While (l < STEPSIZE) And ((l + r) <= ActiveSheet.Cells(65535, "A").End(xlUp).Row)
            Let f = 2 ' first column is added before the loop, 2nd column and after are added in the loop
            Let s = ActiveSheet.Cells(r + l, 1).Value
            While f <= ActiveSheet.Cells(r + l, 255).End(xlToLeft).Column
                s = s & "," & ActiveSheet.Cells(r + l, f).Value
                Let f = f + 1
            Wend
            Print #1, s
            Let l = l + 1
        Wend
        Close #1
        r = r + STEPSIZE
    Wend
End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:Brian Withun
ID: 24861864
If this issue has been resolved, please close it.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question