VBA Code to Split a column into new workbooks

CarlosMD

New Member
Joined
Mar 1, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with one column of 30,000+ rows. I'd like to split that column up every 1,000 rows and put it into a new csv workbook with a header. I'd also like to name the new files in numeral order (ex: first 1000 rows go into a new workbook named "1", next 1000 rows go into a workbook named "2", etc)

Currently using a VBA code to split the column into new xlsx workbooks with no header. This code does not save the new workbooks that are created. I'll attach the code if it's easier to just modify it


VBA CODE:

VBA Code:
Sub split()

    'Set dimensions
    Dim Table As Range, TableArray(), _
        CutValue As Integer, Cntr As Integer, _
        TempArray(), Width As Integer, _
        x As Integer, y As Integer, _
        Height As Long, Rep As Integer, _
        LoopReps As Long

    'Get data
    Set Table = Application.InputBox("Specify range to copy", _
        Default:=ActiveCell.CurrentRegion.Address, Type:=8)
    CutValue = InputBox("How many rows should the chunks be?", _
        Default:=1000)
    Width = 1
    Height = Table.Rows.Count

    'Write to array
    TableArray = Table
    ReDim TempArray(1 To CutValue, 1 To Width)
    Rep = Application.WorksheetFunction.RoundUp(Height / CutValue, 0)
    LoopReps = CLng(CutValue)

    'Loop through all new sheets
    For Cntr = 0 To Rep - 1
        If Height - Cntr * CLng(CutValue) < CLng(CutValue) Then _
            LoopReps = Height - Cntr * CLng(CutValue)

        For x = 1 To Width
            For y = 1 To LoopReps
                TempArray(y, x) = TableArray(y + Cntr * CLng(CutValue), x)
            Next y
        Next x

        Workbooks.Add
        Range("A1").Resize(LoopReps, Width) = TempArray
    Next Cntr

End Sub
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi
Try this code NOT TESTED
VBA Code:
Sub test()
    Dim a As Variant
    Dim i As Long
    a = Cells(1, 1).CurrentRegion
    Sheets.Add.Name = "Temp"
    With ActiveSheet
        For i = 2 To UBound(a) Step 1000
            .Cells(1, 1) = a(1, 1)
            .Cells(2, 1).Resize(1000) = Application.Index(a, Evaluate("row(" & i & ":1000" & ")"), Array(1))
            .SaveAs ThisWorkbook.Path & i & ".csv", xlCSV
        Next
    End With
    Application.DisplayAlerts = False
    Sheets("Temp").Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Hi
Try this code NOT TESTED
VBA Code:
Sub test()
    Dim a As Variant
    Dim i As Long
    a = Cells(1, 1).CurrentRegion
    Sheets.Add.Name = "Temp"
    With ActiveSheet
        For i = 2 To UBound(a) Step 1000
            .Cells(1, 1) = a(1, 1)
            .Cells(2, 1).Resize(1000) = Application.Index(a, Evaluate("row(" & i & ":1000" & ")"), Array(1))
            .SaveAs ThisWorkbook.Path & i & ".csv", xlCSV
        Next
    End With
    Application.DisplayAlerts = False
    Sheets("Temp").Delete
    Application.DisplayAlerts = True
End Sub
Hi Thank you for the response! Unfortunately, this code is just taking the same 1000 rows and saving it into files. So, when I have a header and 31,000 rows it will create 31 files, 30 of which have the same header and 1000 rows (row 1000-1999 from the original list). The 1st file is different. The first 3 rows are the same as the rest and then the rest of the 1000 rows are filled with NAs. Not sure what is causing this problem
 
Upvote 0
Hi
will you try
VBA Code:
Sub test()
    Dim a As Variant
    Dim i As Long
    a = Sheet1.Cells(1, 1).CurrentRegion
    Sheets.Add.Name = "Temp"
    With ActiveSheet
        For i = 2 To UBound(a) Step 999
            .Cells(1, 1) = a(1, 1)
            x = Application.Transpose(Evaluate("row(" & i & ":" & Evaluate(i & " + 999") & ")"))
            .Cells(2, 1).Resize(999) = Application.Transpose(Application.Index(a, x, 1))
            .SaveAs ThisWorkbook.Path & i & ".csv", xlCSV
        Next
    End With
    Application.DisplayAlerts = False
    Sheets("Temp").Delete
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,472
Members
452,516
Latest member
archcalx

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