Divide Database Into Smaller Files with 200 lines each

Gamermatt

Board Regular
Joined
May 14, 2009
Messages
186
Hello,

I have a very large database, and I need to divide it into smaller files of 200 lines each. So, every 200 lines there should be a new file saved. Is there a way to automate this, and save each smaller database as a csv file? The names can just be 1.csv, 2.csv, etc.

Matt
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
This should do the trick, remember to change the file path in the SaveAs and the total rows in the Do Until lRow >= 1197 bit.

Code:
Sub Test()
    Dim wrkBk As Workbook, wrkSht As Worksheet
    Dim lNumber As Long
    Dim lRow As Long
    
    lRow = 1
    lNumber = 1
    
    Application.DisplayAlerts = False
    
    Do Until lRow >= 1197
        Set wrkBk = Workbooks.Add
        With wrkBk
            Set wrkSht = .Worksheets.Add
            wrkSht.Name = lRow & " to " & lRow + 199
            ThisWorkbook.Worksheets("Sheet1").Rows(lRow & ":" & lRow + 199).Copy Destination:=wrkSht.Range("A1")
            .SaveAs "D:\Documents and Settings\My Documents\" & lNumber, xlCSV
            .Close
            lRow = lRow + 200
            lNumber = lNumber + 1
        End With
    Loop

    Application.DisplayAlerts = False
    
End Sub
 
Upvote 0
This should do the trick, remember to change the file path in the SaveAs and the total rows in the Do Until lRow >= 1197 bit.

Code:
Sub Test()
    Dim wrkBk As Workbook, wrkSht As Worksheet
    Dim lNumber As Long
    Dim lRow As Long
    
    lRow = 1
    lNumber = 1
    
    Application.DisplayAlerts = False
    
    Do Until lRow >= 1197
        Set wrkBk = Workbooks.Add
        With wrkBk
            Set wrkSht = .Worksheets.Add
            wrkSht.Name = lRow & " to " & lRow + 199
            ThisWorkbook.Worksheets("Sheet1").Rows(lRow & ":" & lRow + 199).Copy Destination:=wrkSht.Range("A1")
            .SaveAs "D:\Documents and Settings\My Documents\" & lNumber, xlCSV
            .Close
            lRow = lRow + 200
            lNumber = lNumber + 1
        End With
    Loop

    Application.DisplayAlerts = False
    
End Sub

Thank you!:biggrin:
 
Upvote 0
No probs.

Just noticed something tho - the last

Application.DisplayAlerts = False

should be

Application.DisplayAlerts = True

DisplayAlerts allows it to overwrite any existing files of that name without giving a warning message, so you may want to omit those lines.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

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