How to split large excel sheet into two separate text file (.txt)?

JackkG

New Member
Joined
Dec 10, 2014
Messages
42
Hi guys,

I have an excel file with first sheet having large quantity of data, say around 200,000 rows of data. I'm able to save the sheet data to a text file, but due to the amount of data i'm unable to upload this text file into server. Is there any VBA macro to split this sheet data into two text files with headers intact in both files? For example, first sheets with total of 200,000 records split up with 100,000 records in one text file and another 100,000 in another text file, headers will be same in both. Note: headers and rows may change overtime.

Any help with this will be great.

Thanks
Jackal
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Cross posted https://www.excelforum.com/excel-pr...el-sheet-into-two-separate-text-file-txt.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Hi guys,


I've found a code which does what i need, but it doesn't save it in a text file as required. It saves as .txtm file. Any help tweaking the SaveAs statement will be of great help. Thanks! Here is the code.


Code:
Public Sub Split()


    Dim inputFile As String, inputWb As Workbook
    Dim lastRow As Long, row As Long, n As Long
    Dim newCSV As Workbook
    Dim mainFolder As String, saveSubfolder As String
    
    inputFile = "C:\Desktop\Personal\DataTest-1.xlsm"
    
    mainFolder = "C:\Desktop\Personal\"
    If Right(mainFolder, 1) <> "\" Then mainFolder = mainFolder & "\"
    
    Set inputWb = Workbooks.Open(inputFile)
    
    Application.DisplayAlerts = False
    
    With inputWb.Worksheets(1)
        lastRow = .Cells(Rows.Count, "A").End(xlUp).row
        
        Set newCSV = Workbooks.Add
        
        n = 0
        For row = 2 To lastRow Step 55500
            n = n + 1
            .Rows(1).EntireRow.Copy newCSV.Worksheets(1).Range("A1")
            .Rows(row & ":" & row + 55500 - 1).EntireRow.Copy newCSV.Worksheets(1).Range("A2")
            
            saveSubfolder = mainFolder
            On Error Resume Next
            On Error GoTo 0
            newCSV.SaveAs FileName:=saveSubfolder & Replace(inputWb.Name, ".xls", "(" & n & ").txt"), FileFormat:=xlTextWindows, CreateBackup:=False
        Next
    End With
    
    newCSV.Close saveChanges:=False
Application.DisplayAlerts = True
End Sub

Same has been posted here:
https://www.excelforum.com/excel-pr...o-two-separate-text-file-txt.html#post5073326
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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