Macro to Split Excel File in Multiple .csv files every 5000 lines

borjasanz

New Member
Joined
Sep 17, 2013
Messages
5
Hello guys.

I need help with a macro that achieves this: I have files with over 15,000/20,000 lines that are supposed to be uploaded to the system in independent 5000 line .csv files to the SAP system.

However, I have no idea how to do this. I've thought about it and really can't think of any solution via VBA.

Please I need your help! Files have a variable number of lines every day and it takes a lot of time to split them daily and manually.

I also need files to be named this way, imagine the original file is named ABCDE.xlsx, I would need the new files to be named ABCDE(1).csv, ABCDE(2).csv, ABCDE(3).csv, etc. depending on how many lines the file has. The last file can have less than 5,000 lines; just the maximum number has to be 5000.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this:
Code:
Public Sub Split_5000()

    Dim inputFile As String, inputWb As Workbook
    Dim lastRow As Long, row As Long, n As Long
    Dim newCSV As Workbook
    
    inputFile = "C:\Path\To\ABCDE.xlsx"           'CHANGE TO YOUR INPUT FILE, OR USE GETOPENFILENAME
    
    Set inputWb = Workbooks.Open(inputFile)
    
    With inputWb.Worksheets(1)
        lastRow = .Cells(Rows.Count, "A").End(xlUp).row
        
        Set newCSV = Workbooks.Add
        
        n = 0
        For row = 1 To lastRow Step 5000
            n = n + 1
            .Rows(row & ":" & row + 5000 - 1).EntireRow.Copy newCSV.Worksheets(1).Range("A1")
            
            'Save in same folder as input workbook with ".xlsx" replaced by "(n).csv"
            newCSV.SaveAs Filename:=Replace(inputWb.FullName, ".xlsx", "(" & n & ").csv"), FileFormat:=xlCSV, CreateBackup:=False
        Next
    End With
    
    newCSV.Close saveChanges:=False
    inputWb.Close saveChanges:=False
    
End Sub
 
Upvote 0
Try this:
Code:
Public Sub Split_5000_With_Column_Headings()

    Dim inputFile As String, inputWb As Workbook
    Dim lastRow As Long, row As Long, n As Long
    Dim newCSV As Workbook
    
    inputFile = "C:\Path\To\ABCDE.xlsx"           'CHANGE TO YOUR INPUT FILE, OR USE GETOPENFILENAME
    
    Set inputWb = Workbooks.Open(inputFile)
    
    With inputWb.Worksheets(1)
        lastRow = .Cells(Rows.Count, "A").End(xlUp).row
        
        Set newCSV = Workbooks.Add
        
        n = 0
        For row = 2 To lastRow Step 5000
            n = n + 1
            .Rows(1).EntireRow.Copy newCSV.Worksheets(1).Range("A1")
            .Rows(row & ":" & row + 5000 - 1).EntireRow.Copy newCSV.Worksheets(1).Range("A2")
            
            'Save in same folder as input workbook with .xlsx replaced by (n).csv
            newCSV.SaveAs Filename:=Replace(inputWb.FullName, ".xlsx", "(" & n & ").csv"), FileFormat:=xlCSV, CreateBackup:=False
        Next
    End With
    
    newCSV.Close saveChanges:=False
    inputWb.Close saveChanges:=False
    
End Sub
 
Last edited:
Upvote 0
Thanks John_W!!!!

Is there any way that I can save the files to the different folder paths in a range?

I am sorry but if this does not make sense but What I am actually looking for is

Input1 gets saved to C:\Users\Aziz\Desktop\New folder
Input2 gets saved to C:\Users\Aziz\Desktop\New folder1
Input3 gets saved to C:\Users\Aziz\Desktop\New folder2
Input4 gets saved to C:\Users\Aziz\Desktop\New folder3
Input5 gets saved to C:\Users\Aziz\Desktop\New folder4
Input6 gets saved to C:\Users\Aziz\Desktop\New folder5
Input7 gets saved to C:\Users\Aziz\Desktop\New folder6
Input8 gets saved to C:\Users\Aziz\Desktop\New folder7
Input9 gets saved to C:\Users\Aziz\Desktop\New folder8
Input10 gets saved to C:\Users\Aziz\Desktop\New folder9
 
Upvote 0
Which range? The code creates multiple output files, not input files. Assuming you mean output files, and coding according to the second part of your post, try this:
Code:
Public Sub Split_5000_With_Column_Headings_Save_In_Folders()

    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:\Path\To\ABCDE.xls"           'CHANGE TO YOUR INPUT FILE, OR USE GETOPENFILENAME
    
    mainFolder = "C:\Users\Aziz\Desktop\"        'MAIN FOLDER IN WHICH 'New foldern' SUBFOLDERS ARE CREATED AND .CSV FILES ARE SAVED
    If Right(mainFolder, 1) <> "\" Then mainFolder = mainFolder & "\"
    
    Set inputWb = Workbooks.Open(inputFile)
    
    With inputWb.Worksheets(1)
        lastRow = .Cells(Rows.Count, "A").End(xlUp).row
        
        Set newCSV = Workbooks.Add
        
        n = 0
        For row = 2 To lastRow Step 5000
            n = n + 1
            .Rows(1).EntireRow.Copy newCSV.Worksheets(1).Range("A1")
            .Rows(row & ":" & row + 5000 - 1).EntireRow.Copy newCSV.Worksheets(1).Range("A2")
            
            saveSubfolder = mainFolder & "New folder" & n & "\"
            On Error Resume Next
            MkDir saveSubfolder
            On Error GoTo 0
            
            'Save in 'New foldern' subfolder with .xls replaced by (n).csv
            newCSV.SaveAs Filename:=saveSubfolder & Replace(inputWb.Name, ".xls", "(" & n & ").csv"), FileFormat:=xlCSV, CreateBackup:=False
        Next
    End With
    
    newCSV.Close saveChanges:=False
    inputWb.Close saveChanges:=False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

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