Split master data into multiple workbooks with header

ibbara

New Member
Joined
Oct 4, 2023
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello there,

I'm searching for a macro that split data into multiple workbook. The splitted name will be the employer name with maximum of 120 characters in name when saving and having the 8 rows as header.

We are sending billing statement from different company. This will be a very very big help to us


IMG_20231004_134706.png
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Something not clearly, fist i saw that master workbook have some column have been hidden, what do you want to do with those? next, look like some employer name have more than 1 company and it have special symbol "/" so when split to another workbook, what is the name you want to use?
 
Upvote 0
Hello there,

I'm searching for a macro that split data into multiple workbook. The splitted name will be the employer name with maximum of 120 characters in name when saving and having the 8 rows as header.

We are sending billing statement from different company. This will be a very very big help to us


View attachment 99700
Welcome to MrExcel

Have you posted this request twice?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Welcome to MrExcel

Have you posted this request twice?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Welcome to MrExcel

Have you posted this request twice?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
I posted twice due to my internet connection. I'm sorry for that. Ive tried to delete it but idk. Again im very sorry
 
Upvote 0
Something not clearly, fist i saw that master workbook have some column have been hidden, what do you want to do with those? next, look like some employer name have more than 1 company and it have special symbol "/" so when split to another workbook, what is the name you want to use?
Hello,

I hid it to show the employer name. The hidden columns are loan details such as amortization, unpaid monthly amortization, penalty and total payment.

I want to split it per company name or use the company name as file name. Some company name have some special characters and almost have 220 maximum characters.

I have found a vba code but it doesnt have header and merge cell on it. In my files i have 9 rows as header and merged cells for the loan details of employees.

I am sending the loan statement of 5,000 companies.


This will be a big help.

Thank you
 
Upvote 0
Hello,

I hid it to show the employer name. The hidden columns are loan details such as amortization, unpaid monthly amortization, penalty and total payment.

I want to split it per company name or use the company name as file name. Some company name have some special characters and almost have 220 maximum characters.

I have found a vba code but it doesnt have header and merge cell on it. In my files i have 9 rows as header and merged cells for the loan details of employees.

I am sending the loan statement of 5,000 companies.


This will be a big help.

Thank you

Hello,

I hid it to show the employer name. The hidden columns are loan details such as amortization, unpaid monthly amortization, penalty and total payment.

I want to split it per company name or use the company name as file name. Some company name have some special characters and almost have 220 maximum characters.

I have found a vba code but it doesnt have header and merge cell on it. In my files i have 9 rows as header and merged cells for the loan details of employees.

I am sending the loan statement of 5,000 companies.


This will be a big help.

Thank you
 

Attachments

  • IMG_20231005_145414.png
    IMG_20231005_145414.png
    35 KB · Views: 10
Upvote 0
ok, because you have so many employers that means macro will create so many files and working with large data, so i think you should create a list of all employers and create each file for all of them. So i create this code and test it with my example data and it work great. You can try this and feedback to me:
VBA Code:
Sub SplitEmployerList()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim shMaster As Worksheet
    Dim shEmp As Worksheet
    Dim cll As Range
    Dim mRng As Range
    Dim eRng As Range
    Set shMaster = ThisWorkbook.Sheets("LIST")
    If lrow(shMaster, 11) < 10 Then Exit Sub
    Set mRng = shMaster.Range("K10:K" & lrow(shMaster, 11))
    Call GetEmployerList(mRng)
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set eRng = shEmp.Range("A1:A" & lrow(shEmp, 1))
    For Each cll In eRng
        If Not IsEmpty(cll) Then
            Call SplitEmployer(cll.Value, mRng)
        End If
    Next cll
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Private Sub GetEmployerList(ByVal empRng As Range) 'get list of all employers with no duplicate
    Dim cll As Range
    Dim shEmp As Worksheet
    Dim empList As Range
    Dim i As Integer
    Dim emp() As String
    Sheets.Add After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) 'create new sheet to list all employer
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    For Each cll In empRng
        emp = Split(cll.Value, "/") 'split employer when cell has more than 1, separator is "/"
        For i = LBound(emp) To UBound(emp)
            Set empList = shEmp.Range("A1:A" & lrow(shEmp, 1))
            If CustomMatch(Left(emp(i), 220), empList) = False Then
                shEmp.Cells(lrow(shEmp, 1) + 1, 1).Value = Left(emp(i), 220) 'limit with 220 characters
            End If
        Next i
    Next cll
End Sub

Private Sub SplitEmployer(ByVal emp As String, ByVal rng As Range) 'split all employers from master workbook base on sheet employer list
    Dim cll As Range
    Dim i As Integer
    Dim j As Integer
    Dim wbOut As Workbook
    Dim shOut As Worksheet
    Workbooks.Add 'create new workbook
    Set wbOut = ActiveWorkbook
    Set shOut = wbOut.Sheets(1)
    ThisWorkbook.Sheets("LIST").Range("A1:K9").Copy 'copy header of master sheet
    shOut.Cells(1, 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    For Each cll In rng
        If InStr(cll.Value, emp) > 0 Then 'condition when found matched employer
            If lrow(shOut, 11) < 10 Then
                j = 10
            Else
                j = lrow(shOut, 11) + 1
            End If
            For i = 0 To 10
                shOut.Cells(j, 11 - i).Value = cll.Offset(, -i).Value
            Next i
        End If
    Next cll
    wbOut.SaveAs Filename:=ThisWorkbook.Path & "\" & emp & ".xlsx" 'save new workbook with employer name and close
    wbOut.Close
End Sub

Private Function lrow(ByVal sh As Worksheet, ByVal col As Integer) As Long 'find last row of table
    lrow = sh.Cells(Rows.Count, col).End(xlUp).Row
End Function

Private Function CustomMatch(ByVal xVal As String, ByVal rng As Range) As Boolean 'find match employer
    Dim cll As Range
    For Each cll In rng
        If cll.Value = xVal Then
            CustomMatch = True
            Exit For
        Else
            CustomMatch = False
        End If
    Next cll
End Function
 
Upvote 0
ok, because you have so many employers that means macro will create so many files and working with large data, so i think you should create a list of all employers and create each file for all of them. So i create this code and test it with my example data and it work great. You can try this and feedback to me:
VBA Code:
Sub SplitEmployerList()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim shMaster As Worksheet
    Dim shEmp As Worksheet
    Dim cll As Range
    Dim mRng As Range
    Dim eRng As Range
    Set shMaster = ThisWorkbook.Sheets("LIST")
    If lrow(shMaster, 11) < 10 Then Exit Sub
    Set mRng = shMaster.Range("K10:K" & lrow(shMaster, 11))
    Call GetEmployerList(mRng)
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set eRng = shEmp.Range("A1:A" & lrow(shEmp, 1))
    For Each cll In eRng
        If Not IsEmpty(cll) Then
            Call SplitEmployer(cll.Value, mRng)
        End If
    Next cll
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Private Sub GetEmployerList(ByVal empRng As Range) 'get list of all employers with no duplicate
    Dim cll As Range
    Dim shEmp As Worksheet
    Dim empList As Range
    Dim i As Integer
    Dim emp() As String
    Sheets.Add After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) 'create new sheet to list all employer
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    For Each cll In empRng
        emp = Split(cll.Value, "/") 'split employer when cell has more than 1, separator is "/"
        For i = LBound(emp) To UBound(emp)
            Set empList = shEmp.Range("A1:A" & lrow(shEmp, 1))
            If CustomMatch(Left(emp(i), 220), empList) = False Then
                shEmp.Cells(lrow(shEmp, 1) + 1, 1).Value = Left(emp(i), 220) 'limit with 220 characters
            End If
        Next i
    Next cll
End Sub

Private Sub SplitEmployer(ByVal emp As String, ByVal rng As Range) 'split all employers from master workbook base on sheet employer list
    Dim cll As Range
    Dim i As Integer
    Dim j As Integer
    Dim wbOut As Workbook
    Dim shOut As Worksheet
    Workbooks.Add 'create new workbook
    Set wbOut = ActiveWorkbook
    Set shOut = wbOut.Sheets(1)
    ThisWorkbook.Sheets("LIST").Range("A1:K9").Copy 'copy header of master sheet
    shOut.Cells(1, 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    For Each cll In rng
        If InStr(cll.Value, emp) > 0 Then 'condition when found matched employer
            If lrow(shOut, 11) < 10 Then
                j = 10
            Else
                j = lrow(shOut, 11) + 1
            End If
            For i = 0 To 10
                shOut.Cells(j, 11 - i).Value = cll.Offset(, -i).Value
            Next i
        End If
    Next cll
    wbOut.SaveAs Filename:=ThisWorkbook.Path & "\" & emp & ".xlsx" 'save new workbook with employer name and close
    wbOut.Close
End Sub

Private Function lrow(ByVal sh As Worksheet, ByVal col As Integer) As Long 'find last row of table
    lrow = sh.Cells(Rows.Count, col).End(xlUp).Row
End Function

Private Function CustomMatch(ByVal xVal As String, ByVal rng As Range) As Boolean 'find match employer
    Dim cll As Range
    For Each cll In rng
        If cll.Value = xVal Then
            CustomMatch = True
            Exit For
        Else
            CustomMatch = False
        End If
    Next cll
End Function
Hello,
It worked, thanks a lot for this.

How can i add autofit on columns and the format
ok, because you have so many employers that means macro will create so many files and working with large data, so i think you should create a list of all employers and create each file for all of them. So i create this code and test it with my example data and it work great. You can try this and feedback to me:
VBA Code:
Sub SplitEmployerList()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim shMaster As Worksheet
    Dim shEmp As Worksheet
    Dim cll As Range
    Dim mRng As Range
    Dim eRng As Range
    Set shMaster = ThisWorkbook.Sheets("LIST")
    If lrow(shMaster, 11) < 10 Then Exit Sub
    Set mRng = shMaster.Range("K10:K" & lrow(shMaster, 11))
    Call GetEmployerList(mRng)
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set eRng = shEmp.Range("A1:A" & lrow(shEmp, 1))
    For Each cll In eRng
        If Not IsEmpty(cll) Then
            Call SplitEmployer(cll.Value, mRng)
        End If
    Next cll
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Private Sub GetEmployerList(ByVal empRng As Range) 'get list of all employers with no duplicate
    Dim cll As Range
    Dim shEmp As Worksheet
    Dim empList As Range
    Dim i As Integer
    Dim emp() As String
    Sheets.Add After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) 'create new sheet to list all employer
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    For Each cll In empRng
        emp = Split(cll.Value, "/") 'split employer when cell has more than 1, separator is "/"
        For i = LBound(emp) To UBound(emp)
            Set empList = shEmp.Range("A1:A" & lrow(shEmp, 1))
            If CustomMatch(Left(emp(i), 220), empList) = False Then
                shEmp.Cells(lrow(shEmp, 1) + 1, 1).Value = Left(emp(i), 220) 'limit with 220 characters
            End If
        Next i
    Next cll
End Sub

Private Sub SplitEmployer(ByVal emp As String, ByVal rng As Range) 'split all employers from master workbook base on sheet employer list
    Dim cll As Range
    Dim i As Integer
    Dim j As Integer
    Dim wbOut As Workbook
    Dim shOut As Worksheet
    Workbooks.Add 'create new workbook
    Set wbOut = ActiveWorkbook
    Set shOut = wbOut.Sheets(1)
    ThisWorkbook.Sheets("LIST").Range("A1:K9").Copy 'copy header of master sheet
    shOut.Cells(1, 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    For Each cll In rng
        If InStr(cll.Value, emp) > 0 Then 'condition when found matched employer
            If lrow(shOut, 11) < 10 Then
                j = 10
            Else
                j = lrow(shOut, 11) + 1
            End If
            For i = 0 To 10
                shOut.Cells(j, 11 - i).Value = cll.Offset(, -i).Value
            Next i
        End If
    Next cll
    wbOut.SaveAs Filename:=ThisWorkbook.Path & "\" & emp & ".xlsx" 'save new workbook with employer name and close
    wbOut.Close
End Sub

Private Function lrow(ByVal sh As Worksheet, ByVal col As Integer) As Long 'find last row of table
    lrow = sh.Cells(Rows.Count, col).End(xlUp).Row
End Function

Private Function CustomMatch(ByVal xVal As String, ByVal rng As Range) As Boolean 'find match employer
    Dim cll As Range
    For Each cll In rng
        If cll.Value = xVal Then
            CustomMatch = True
            Exit For
        Else
            CustomMatch = False
        End If
    Next cll
End Function
Hello, It worked, Thank you very much. I appreciated it.

How can i add a code that autofit the columns and the id number is formatted as number with zero decimals?

I Attached the splitted file. The i.d number is formatted as general, some cell in the header do not have boarder below.


Sorry this too much of a request but this will give me easy way to do my work.

Thank you very much and more power to you
 

Attachments

  • IMG_20231006_101622.png
    IMG_20231006_101622.png
    8.9 KB · Views: 3
Upvote 0
Hello,
It worked, thanks a lot for this.

How can i add autofit on columns and the format

Hello, It worked, Thank you very much. I appreciated it.

How can i add a code that autofit the columns and the id number is formatted as number with zero decimals?

I Attached the splitted file. The i.d number is formatted as general, some cell in the header do not have boarder below.


Sorry this too much of a request but this will give me easy way to do my work.

Thank you very much and more power to you
look like your id number in your master sheet formatted as text so i change format of whole split sheet to text, is that OK? i change sub SplitEmployer like this:
VBA Code:
Private Sub SplitEmployer(ByVal emp As String, ByVal rng As Range) 'split all employers from master workbook base on sheet employer list
    Dim cll As Range
    Dim i As Integer
    Dim j As Integer
    Dim wbOut As Workbook
    Dim shOut As Worksheet
    Workbooks.Add 'create new workbook
    Set wbOut = ActiveWorkbook
    Set shOut = wbOut.Sheets(1)
    shOut.Cells.NumberFormat = "@" 'format cells as text
    ThisWorkbook.Sheets("LIST").Range("A1:K9").Copy 'copy header of master sheet
    shOut.Cells(1, 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    For Each cll In rng
        If InStr(cll.Value, emp) > 0 Then 'condition when found matched employer
            If lrow(shOut, 11) < 10 Then
                j = 10
            Else
                j = lrow(shOut, 11) + 1
            End If
            For i = 0 To 10
                shOut.Cells(j, 11 - i).Value = Format(cll.Offset(, -i).Value, "@")
            Next i
        End If
    Next cll
    shOut.Columns("A:K").EntireColumn.AutoFit 'autofit columns width
    wbOut.SaveAs Filename:=ThisWorkbook.Path & "\" & emp & ".xlsx" 'save new workbook with employer name and close
    wbOut.Close
End Sub
 
Upvote 0
ok, because you have so many employers that means macro will create so many files and working with large data, so i think you should create a list of all employers and create each file for all of them. So i create this code and test it with my example data and it work great. You can try this and feedback to me:
VBA Code:
Sub SplitEmployerList()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim shMaster As Worksheet
    Dim shEmp As Worksheet
    Dim cll As Range
    Dim mRng As Range
    Dim eRng As Range
    Set shMaster = ThisWorkbook.Sheets("LIST")
    If lrow(shMaster, 11) < 10 Then Exit Sub
    Set mRng = shMaster.Range("K10:K" & lrow(shMaster, 11))
    Call GetEmployerList(mRng)
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set eRng = shEmp.Range("A1:A" & lrow(shEmp, 1))
    For Each cll In eRng
        If Not IsEmpty(cll) Then
            Call SplitEmployer(cll.Value, mRng)
        End If
    Next cll
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Private Sub GetEmployerList(ByVal empRng As Range) 'get list of all employers with no duplicate
    Dim cll As Range
    Dim shEmp As Worksheet
    Dim empList As Range
    Dim i As Integer
    Dim emp() As String
    Sheets.Add After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) 'create new sheet to list all employer
    Set shEmp = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    For Each cll In empRng
        emp = Split(cll.Value, "/") 'split employer when cell has more than 1, separator is "/"
        For i = LBound(emp) To UBound(emp)
            Set empList = shEmp.Range("A1:A" & lrow(shEmp, 1))
            If CustomMatch(Left(emp(i), 220), empList) = False Then
                shEmp.Cells(lrow(shEmp, 1) + 1, 1).Value = Left(emp(i), 220) 'limit with 220 characters
            End If
        Next i
    Next cll
End Sub

Private Sub SplitEmployer(ByVal emp As String, ByVal rng As Range) 'split all employers from master workbook base on sheet employer list
    Dim cll As Range
    Dim i As Integer
    Dim j As Integer
    Dim wbOut As Workbook
    Dim shOut As Worksheet
    Workbooks.Add 'create new workbook
    Set wbOut = ActiveWorkbook
    Set shOut = wbOut.Sheets(1)
    ThisWorkbook.Sheets("LIST").Range("A1:K9").Copy 'copy header of master sheet
    shOut.Cells(1, 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    For Each cll In rng
        If InStr(cll.Value, emp) > 0 Then 'condition when found matched employer
            If lrow(shOut, 11) < 10 Then
                j = 10
            Else
                j = lrow(shOut, 11) + 1
            End If
            For i = 0 To 10
                shOut.Cells(j, 11 - i).Value = cll.Offset(, -i).Value
            Next i
        End If
    Next cll
    wbOut.SaveAs Filename:=ThisWorkbook.Path & "\" & emp & ".xlsx" 'save new workbook with employer name and close
    wbOut.Close
End Sub

Private Function lrow(ByVal sh As Worksheet, ByVal col As Integer) As Long 'find last row of table
    lrow = sh.Cells(Rows.Count, col).End(xlUp).Row
End Function

Private Function CustomMatch(ByVal xVal As String, ByVal rng As Range) As Boolean 'find match employer
    Dim cll As Range
    For Each cll In rng
        If cll.Value = xVal Then
            CustomMatch = True
            Exit For
        Else
            CustomMatch = False
        End If
    Next cll
End Function

look like your id number in your master sheet formatted as text so i change format of whole split sheet to text, is that OK? i change sub SplitEmployer like this:
VBA Code:
Private Sub SplitEmployer(ByVal emp As String, ByVal rng As Range) 'split all employers from master workbook base on sheet employer list
    Dim cll As Range
    Dim i As Integer
    Dim j As Integer
    Dim wbOut As Workbook
    Dim shOut As Worksheet
    Workbooks.Add 'create new workbook
    Set wbOut = ActiveWorkbook
    Set shOut = wbOut.Sheets(1)
    shOut.Cells.NumberFormat = "@" 'format cells as text
    ThisWorkbook.Sheets("LIST").Range("A1:K9").Copy 'copy header of master sheet
    shOut.Cells(1, 1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme
    For Each cll In rng
        If InStr(cll.Value, emp) > 0 Then 'condition when found matched employer
            If lrow(shOut, 11) < 10 Then
                j = 10
            Else
                j = lrow(shOut, 11) + 1
            End If
            For i = 0 To 10
                shOut.Cells(j, 11 - i).Value = Format(cll.Offset(, -i).Value, "@")
            Next i
        End If
    Next cll
    shOut.Columns("A:K").EntireColumn.AutoFit 'autofit columns width
    wbOut.SaveAs Filename:=ThisWorkbook.Path & "\" & emp & ".xlsx" 'save new workbook with employer name and close
    wbOut.Close
End Sub
Hello eiloken its perfectly working i am so very thankful. Thank you very much.

Can i ask what code will i change if i have headings from A6 to Q6? I'm trying to change the code but its not working. I'm not that knowledgeable ing vba.
Thank you very much ✌️
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,277
Members
449,093
Latest member
Vincent Khandagale

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