Split rows separated by blank row and create multiple files

PANIGGR

New Member
Joined
Sep 4, 2015
Messages
20
I have a tab with more than 10000 rows separated by block of data separated by a blank row. Each block has header. I need multiple files based on each block of data based on the column A. Each file to be named based on column F (Payment Amt)

sample data as follows,

HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D1029290387150227551,186.7410/18/202465094198001,186.74Basic Remittance1
D Basic Remittance2
D Basic Remittance3
D Basic Remittance4
HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D10292903971800572651.9510/18/2024650941980051.95Basic Remittance1
D Basic Remittance2
HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D10289375175006467.310/18/2024650902258067.30Basic Remittance1
HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D102930977635884623.3110/18/20246509449720623.31Expanded1
D Expanded2
D Expanded3
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I have a tab with more than 10000 rows separated by block of data separated by a blank row. Each block has header. I need multiple files based on each block of data based on the column A. Each file to be named based on column F (Payment Amt)

sample data as follows,

HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D1029290387150227551,186.7410/18/202465094198001,186.74Basic Remittance1
DBasic Remittance2
DBasic Remittance3
DBasic Remittance4
HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D10292903971800572651.9510/18/2024650941980051.95Basic Remittance1
DBasic Remittance2
HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D10289375175006467.310/18/2024650902258067.30Basic Remittance1
HDPAPayment NumberPayment AmtSettlement DatePayment File IDProcessing FeesAmount Paid to CollectorRemittance TypeRemit Number
D102930977635884623.3110/18/20246509449720623.31Expanded1
DExpanded2
DExpanded3
Try this on a copy of your data.

There are a few changes that you will need to make.

These are indicated at the start of the code.

VBA Code:
Public Sub subSplitSaveData()
Dim strPath As String
Dim rng As Range
Dim Ws As Worksheet
Dim intCount As Integer
Dim strSheetName As String

  ActiveWorkbook.Save

  ' Change 'Data' to the name of the sheet containing the data.
  Set Ws = Worksheets("Data")
  
  ' Change this to the path where the new workbooks are to be saved.
  strPath = ActiveWorkbook.Path & "\"
    
  ' Change 'Data' to what the new workbook sheet is be named.
  strSheetName = "Data"
  
  Application.DisplayAlerts = False
  
  On Error Resume Next
  Worksheets("Split").Delete
  On Error GoTo 0
  Worksheets.Add after:=Ws
  ActiveSheet.Name = "Split"
  
  Ws.Range("A1").EntireRow.Insert

  Application.ScreenUpdating = False

  For Each rng In Ws.UsedRange.Columns(1).Cells
  
    If IsEmpty(rng) Then
      
      With Worksheets("Split")
        .Cells.Clear
        rng.Offset(1, 0).CurrentRegion.Copy .Range("A1")
        .Copy
        With ActiveWorkbook
          With .Sheets(1)
            .Name = strSheetName
            .Cells.EntireColumn.AutoFit
          End With
          On Error Resume Next
          Kill (strPath & rng.Offset(2, 5))
          On Error GoTo 0
          .SaveAs strPath & rng.Offset(2, 5)
          .Close False
        End With
        intCount = intCount + 1
      End With
     
    End If
  
  Next rng
  
  Ws.Range("A1").EntireRow.Delete
  
  Application.DisplayAlerts = True
  
  Application.ScreenUpdating = True
  
  Ws.Activate

  MsgBox intCount & " workbooks created.", vbOKOnly, "Confirmation"

End Sub
 
Upvote 0
Edit - posted just few seconds later :-) But uses a bit different approach
You could use such macro:
VBA Code:
Sub separatefiles()
Dim i As Long, j As Long, fname As String
Application.ScreenUpdating = False
j = Cells(Rows.Count, "J").End(xlUp).Row
While j > 1
  i = Cells(j, "J").End(xlUp).Row
  fname = Replace(Cells(i + 1, "D"), ",", "_") 'just in case you have comma in amount - it is not allowed in filename
  Range(Cells(i, "A"), Cells(j, "K")).Copy
  Workbooks.Add
  ActiveSheet.Paste
  ActiveWorkbook.SaveAs Filename:=fname & ".xlsx"
  ActiveWorkbook.Close False
  j = i - 2
Wend
Application.ScreenUpdating = True
End Sub

Final comments - I noticed in sample date the amount is in columnD not E. If it's in E correct this line
VBA Code:
  fname = Replace(Cells(i + 1, "E"), ",", "_") 'just in case you have comma in amount - it is not allowed in filename

The resulting files have standard sheet1 name.
You start the macro having your sheet with data to be splitted open.
The files will be saved to default folder. If you need to change it add the path to this line, in a similar way (use your folder path :-):
VBA Code:
  ActiveWorkbook.SaveAs Filename:= "C:\Users\Kaper\Test\" & fname & ".xlsx"
 
Last edited:
Upvote 0
Solution
Edit - few seconds before posting I saw a message that the answer was posted.
Anyway ... I'm not deleting it, because it uses a bit different approach.

Final comment - both codes will be not-very-quick. So test on a small sample (may be some 10 records). If it works as expected - delete produced files and rerun on whole data. Probably there will be a time for a coffe or for a quick phone call :cool: while 10 000 rows are processed
 
Upvote 0
Edit - posted just few seconds later :-) But uses a bit different approach
You could use such macro:
VBA Code:
Sub separatefiles()
Dim i As Long, j As Long, fname As String
Application.ScreenUpdating = False
j = Cells(Rows.Count, "J").End(xlUp).Row
While j > 1
  i = Cells(j, "J").End(xlUp).Row
  fname = Replace(Cells(i + 1, "D"), ",", "_") 'just in case you have comma in amount - it is not allowed in filename
  Range(Cells(i, "A"), Cells(j, "K")).Copy
  Workbooks.Add
  ActiveSheet.Paste
  ActiveWorkbook.SaveAs Filename:=fname & ".xlsx"
  ActiveWorkbook.Close False
  j = i - 2
Wend
Application.ScreenUpdating = True
End Sub

Final comments - I noticed in sample date the amount is in columnD not E. If it's in E correct this line
VBA Code:
  fname = Replace(Cells(i + 1, "E"), ",", "_") 'just in case you have comma in amount - it is not allowed in filename

The resulting files have standard sheet1 name.
You start the macro having your sheet with data to be splitted open.
The files will be saved to default folder. If you need to change it add the path to this line, in a similar way (use your folder path :-):
VBA Code:
  ActiveWorkbook.SaveAs Filename:= "C:\Users\Kaper\Test\" & fname & ".xlsx"
Always good to see different approaches Kaper.

I assumed that the filename was taken from column F, Payment File ID.
This is, hopefully, unique.

Paniggr did say Payment Amt though.

This process will take a long time on more than 10000 rows.
 
Upvote 0
Edit - few seconds before posting I saw a message that the answer was posted.
Anyway ... I'm not deleting it, because it uses a bit different approach.

Final comment - both codes will be not-very-quick. So test on a small sample (may be some 10 records). If it works as expected - delete produced files and rerun on whole data. Probably there will be a time for a coffe or for a quick phone call :cool: while 10 000 rows are processed
It worked, i tweaked the column reference to rename the file name for each block based on Payment Number. Is it possible to give the file name with Amount and Payment Number from two columns, like 50043547 and 142.53
 
Last edited:
Upvote 0
To have name created from columns C (pmt nr) and D (pmt amt) you may use for instance such ammended line of code with fname (filename) creation:
VBA Code:
  fname = Cells(i + 1, "C") & "_" & Replace(Cells(i + 1, "D"), ",", "_")
 
Upvote 0
To have name created from columns C (pmt nr) and D (pmt amt) you may use for instance such ammended line of code with fname (filename) creation:
VBA Code:
  fname = Cells(i + 1, "C") & "_" & Replace(Cells(i + 1, "D"), ",", "_")
Thank you so much
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,089
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