Splitting a large Worksheet into multiple work books after n rows while keeping the header row

Zaher503

New Member
Joined
Oct 11, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a large dataset of only 3 columns. I want this set to be split every x rows into a separate excel file, or workbook. I also want the header row to remain the same in all files.
If you want a sample of what I am looking for I can provide some screenshots.

I have looked everywhere without a solution so any help would be great.
 
1- How would you customize the x factor except to amend the VBA procedure ?​
5 : Is it always the same original text filename in the same original directory (in that case I need the full path)​
or need to choose the file each time you launch the VBA procedure ?​
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
1- How would you customize the x factor except to amend the VBA procedure ?​
5 : Is it always the same original text filename in the same original directory (in that case I need the full path)​
or need to choose the file each time you launch the VBA procedure ?​
1- If you show me where in the code that number is I can edit it myself for each file. Or maybe a pop up that asks you how many lines you want this time? It doesn't matter really.

5- It will be the same filename every time. to make things easier I will rename every file I want to use this code on to split.xlsx

Regarding the path it is going to be on my desktop. Here is the path:
C:\Users\Zaher\Desktop\Work Docs
 
Upvote 0
So the full path should be every time C:\Users\Zaher\Desktop\Work Docs\Sample.csv ?​
 
Upvote 0

Ok, now I have enough material to give it a try, as a reminder for your next thread …​
 
Upvote 0
According to your attachment a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
   Const D="C:\Users\Zaher\Desktop\Work Docs\", F="Sample.csv", P = D & "Output\"
     Dim M&, Ws As Worksheet, R&, N&
         If Dir(D & F) = "" Then Beep: Exit Sub
         If Dir(P, 16) = "" Then MkDir P
    With Application
      Do
         M = .InputBox("Max rows # ?", "Split csv to xlsx", 100, , , , , 1) - 1
         If M = -1 Then Exit Sub Else If M < 1 Then Beep
      Loop Until M > 0
        .DisplayAlerts = False
        .ScreenUpdating = False
        .SheetsInNewWorkbook = 1
         Set Ws = Workbooks.Add.Sheets(1)
    With Workbooks.Open(D & F, , , 2).Sheets(1).UsedRange.Rows
           .Item(1).Copy Ws.[A1]
        For R = 2 To .Count Step M
            N = N + 1
            Application.StatusBar = "       Workbook #" & N
           .Item(R).Resize(M).Copy Ws.[A2]
            Ws.Columns.AutoFit
            Ws.Parent.SaveAs P & Format(N, "000"), 51
            If N Mod 6 = 0 Then DoEvents
        Next
           .Parent.Parent.Close
    End With
        Ws.Parent.Close
        Set Ws = Nothing
       .DisplayAlerts = True
       .ScreenUpdating = True
       .StatusBar = False
    End With
        MsgBox N & " workbooks saved", 64, "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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