Split file into multiple files based on year in column

Hasson

Active Member
Joined
Apr 8, 2021
Messages
401
Office Version
  1. 2016
Platform
  1. Windows
Hi

I have multiple sheets in file contains dates in column B . what I look for split file into multiple files based on YEAR without forgetting create headers in row1 for each sheet based on source file and shouldn't delete the formatting , borders for sheets as in source file when splitting.
should rename file based on year with add FILE word like FILE_2022,FILE_2023.

when splitting file should be in the same folder contains source file.
here is sample.
source
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/01/2022HASSONBTS00INV-00120.00120.002,400.00
3201/01/2022HASSONBTS01INV-00150.00110.005,500.00
4301/01/2022HASSONBTS02INV-00150.00221.0011,050.00
5401/01/2022HASSONBTS03INV-00160.00122.007,320.00
6BALANCEINV-00126,270.00
7101/01/2022HUSSNIBTS01INV-00270.00120.008,400.00
8201/01/2022HUSSNIBTS02INV-00280.00115.009,200.00
9301/01/2022HUSSNIBTS00INV-00260.00112.006,720.00
10BALANCEINV-00224,320.00
11103/01/2023HASSONBTS01INV-00380.00122.009,760.00
12204/01/2023HASSONBTS02INV-00380.00111.008,880.00
13305/01/2023HASSONBTS00INV-00380.00120.009,600.00
14BALANCE06/01/2023INV-00328,240.00
15103/02/2023HOSSAMBTS01INV-004200.00110.0022,000.00
16204/02/2023HOSSAMBTS02INV-004100.00120.0012,000.00
17305/02/2023HOSSAMBTS00INV-004120.0012.001,440.00
18BALANCEINV-00435,440.00
sh
Cell Formulas
RangeFormula
H15:H17,H11:H13,H7:H9,H2:H5H2=F2*G2
H6H6=SUM(H2:H5)
H10,H18,H14H10=SUM(H7:H9)

so when split into 2022,2023 should be like this
file_2022
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/01/2022HASSONBTS00INV-00120.00120.002,400.00
3201/01/2022HASSONBTS01INV-00150.00110.005,500.00
4301/01/2022HASSONBTS02INV-00150.00221.0011,050.00
5401/01/2022HASSONBTS03INV-00160.00122.007,320.00
6BALANCEINV-00126,270.00
7101/01/2022HUSSNIBTS01INV-00270.00120.008,400.00
8201/01/2022HUSSNIBTS02INV-00280.00115.009,200.00
9301/01/2022HUSSNIBTS00INV-00260.00112.006,720.00
10BALANCEINV-00224,320.00
sh
Cell Formulas
RangeFormula
H7:H9,H2:H5H2=F2*G2
H6H6=SUM(H2:H5)
H10H10=SUM(H7:H9)



file_2023
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2103/01/2023HASSONBTS01INV-00380.00122.009,760.00
3204/01/2023HASSONBTS02INV-00380.00111.008,880.00
4305/01/2023HASSONBTS00INV-00380.00120.009,600.00
5BALANCEINV-00328,240.00
6103/02/2023HOSSAMBTS01INV-004200.00110.0022,000.00
7204/02/2023HOSSAMBTS02INV-004100.00120.0012,000.00
8305/02/2023HOSSAMBTS00INV-004120.0012.001,440.00
9BALANCEINV-00435,440.00
sh
Cell Formulas
RangeFormula
H6:H8,H2:H4H2=F2*G2
H5,H9H5=SUM(H2:H4)
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try the following macro. It is very long since it considers many things, it is likely that some detail is missing, in that case provide more examples, just as you put in the original post.

Put the macro in the file that has the data.
VBA Code:
Sub Split_File_Into_Multiple_Files()
  Dim wb2 As Workbook, wb1 As Workbook
  Dim sh As Worksheet
  Dim dic As Object, ky As Variant
  Dim a() As Variant, b() As Variant, c() As Variant
  Dim shfrm As String, nameAnt As String
  Dim nCont As Long, nYear As Long
  Dim i%, j%, k%, n%, lr%

  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Application.StatusBar = False
  
  Set wb1 = ThisWorkbook
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each sh In wb1.Sheets
    If sh.Range("A1").Value = "ITEM" Then
      If shfrm = "" Then shfrm = sh.Name
      nCont = nCont + sh.Range("A" & Rows.Count).End(3).Row
    End If
  Next
  
  ReDim b(1 To nCont, 1 To 9)
  For Each sh In Sheets
    Erase a
    If sh.Range("A1").Value = "ITEM" Then
      a = sh.Range("A2", sh.Range("H" & Rows.Count).End(3)).Value
      For i = 1 To UBound(a, 1)
        If a(i, 3) <> "" Then
          k = k + 1
          nYear = Year(a(i, 2))
          dic(nYear) = Empty
          For j = 1 To UBound(a, 2)
            b(k, j) = a(i, j)
          Next
          b(k, 9) = nYear
        End If
      Next
    End If
  Next
  
  Set wb2 = Workbooks.Add(xlWBATWorksheet)
  With wb2.Sheets(1)
    wb1.Sheets(shfrm).Rows(1).Copy .Range("A1")
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    .Range("A1:I" & UBound(b, 1)).Sort .Range("I2"), xlAscending, .Range("C2"), , xlAscending, .Range("B2"), xlAscending, xlYes
    Erase a
    a = .Range("A2", Range("I" & Rows.Count).End(3)).Value
  End With
  wb2.Close False
  
  For Each ky In dic.keys
    Application.StatusBar = "Creating book: " & ky
    k = 0
    ReDim c(1 To UBound(a, 1), 1 To 8)
    
    For i = 1 To UBound(a, 1)
      If Year(a(i, 2)) = ky Then
        k = k + 1
        For j = 1 To UBound(a, 2) - 1
          c(k, j) = a(i, j)
        Next
      End If
    Next
  
    nameAnt = c(1, 3)
    k = 0
    n = 0
  
    ReDim b(1 To UBound(c, 1) + 1000, 1 To 8)
    For i = 1 To UBound(c, 1)
  
      If nameAnt <> c(i, 3) Then        'corte nombre
        k = k + 1
        b(k, 1) = "BALANCE"
        b(k, 8) = tot
        tot = 0
        n = 0
        If c(i, 3) = "" Then Exit For
      End If
      
      tot = tot + c(i, 8)
      k = k + 1
      n = n + 1
      b(k, 1) = n
      For j = 2 To UBound(c, 2)
        b(k, j) = c(i, j)
      Next
      nameAnt = c(i, 3)
    Next
    
    Set wb2 = Workbooks.Add(xlWBATWorksheet)
    With wb2.Sheets(1)
      wb1.Sheets(shfrm).Range("A1:H1").Copy
      .Range("A1").PasteSpecial xlPasteAll
      .Range("A1").PasteSpecial xlPasteColumnWidths
      .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
      lr = .Range("A" & Rows.Count).End(3).Row
      wb1.Sheets(shfrm).Range("A2").Copy
      .Range("A2:E" & lr).PasteSpecial xlPasteFormats
      wb1.Sheets(shfrm).Range("B2").Copy
      .Range("B2:B" & lr).PasteSpecial xlPasteFormats
      wb1.Sheets(shfrm).Range("H2").Copy
      .Range("F2:H" & lr).PasteSpecial xlPasteFormats
      
      Application.FindFormat.Clear
      Application.ReplaceFormat.Clear
      With Application.ReplaceFormat.Font
          .FontStyle = "Bold"
          .Subscript = False
          .Color = 255
          .TintAndShade = 0
      End With
      .Range("A:A").Replace "BALANCE", "BALANCE", xlWhole, , False, False, SearchFormat:=True
    End With

    wb2.SaveAs wb1.Path & "\" & "FILE_" & ky, xlOpenXMLWorkbook
    wb2.Close False
  Next
  
  Application.StatusBar = False
  Application.FindFormat.Clear
  Application.ReplaceFormat.Clear
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub
Note: In the lower left corner you can see the progress in the creation of the books.

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
🤗
 
Upvote 0
Hi,
It is very long since it considers many things, it is likely that some detail is missing
I 'm not sure what I should post more details !
thanks for the code, I tested , but I note if I have multiple sheets contains the same year then will merge in the same sheet , this is not what I want .
should show the same year for each sheet separately as in the file source also should show the same sheets names as in file source after splitting.
for instance in file source I have sheets names (sh,main,data) and as example if sheets (sh,data) contains 2022 when split data
file 2022 should data for 2022 year for two sheets as in source file sheets (sh,data) .
 
Upvote 0
I 'm not sure what I should post more details !

I am precisely referring to your following comment:
I note if I have multiple sheets contains the same year then will merge in the same sheet

In your example you only put one sheet in the source book and you did not explain whether you want the other sheets separately or together.

Then I ask you to put the complete example with 2 or more sheets in the source file.

That way I wouldn't have to guess the outcome.
🧙‍♂️
 
Upvote 0
OK
here is sheets in source file
report.xlsm
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/01/2022HASSONBTS00INV-001201202400
3201/01/2022HASSONBTS01INV-001501105500
4301/01/2022HASSONBTS02INV-0015022111050
5401/01/2022HASSONBTS03INV-001601227320
6BALANCE26270
7101/01/2022HUSSNIBTS01INV-002701208400
8201/01/2022HUSSNIBTS02INV-002801159200
9301/01/2022HUSSNIBTS00INV-002601126720
10BALANCE24320
11103/01/2023HASSONBTS01INV-003801229760
12204/01/2023HASSONBTS02INV-003801118880
13305/01/2023HASSONBTS00INV-003801209600
14BALANCE28240
15103/02/2023HOSSAMBTS01INV-00420011022000
16204/02/2023HOSSAMBTS02INV-00410012012000
17305/02/2023HOSSAMBTS00INV-004120121440
18BALANCE35440
SH
Cell Formulas
RangeFormula
H15:H17,H11:H13,H7:H9,H2:H5H2=F2*G2
H6H6=SUM(H2:H5)
H10,H18,H14H10=SUM(H7:H9)


report.xlsm
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/01/2022HASSON1BFFG1INVO-00201202400
3201/01/2022HASSON2BFFG2INVO-00501105500
4301/01/2022HASSON3BFFG3INVO-005022111050
5401/01/2022HASSON4BFFG4INVO-00601227320
6BALANCE26270
7101/01/2022HUSSNIBTS01INVO-01701208400
8201/01/2022HUSSNIBTS02INVO-01801159200
9301/01/2022HUSSNIBTS00INVO-01601126720
10BALANCE24320
11103/01/2023HASSONBTS01INVO-02801229760
12204/01/2023HASSONBTS02INVO-02801118880
13305/01/2023HASSONBTS00INVO-02801209600
14BALANCE28240
15103/02/2024HOSSAMBTS01INVO-0320011022000
16203/02/2024HOSSAMBTS02INVO-0310012012000
17303/02/2024HOSSAMBTS00INVO-03120121440
18BALANCE35440
mm
Cell Formulas
RangeFormula
H15:H17,H11:H13,H7:H9,H2:H5H2=F2*G2
H6H6=SUM(H2:H5)
H10,H18,H14H10=SUM(H7:H9)


report.xlsm
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/02/2022HASSONBVAC1AN001201222440
3201/02/2022HASSONBVAC2AN001101121120
4301/02/2022HASSONBVAC3AN001122222664
5BALANCE6224
6101/03/2022HOSSAMVBD1AN002101251250
7201/03/2022HOSSAMVBD2AN002101151150
8301/03/2022HOSSAMVBD3AN002201122240
9BALANCE4640
10101/03/2023AMURRIVBD4AN003201202400
11201/03/2023AMURRIVBD5AN003121221464
12301/03/2023AMURRIVBD6AN003111111221
13BALANCE5085
main
Cell Formulas
RangeFormula
H10:H12,H6:H8,H2:H4H2=F2*G2
H5,H13,H9H5=SUM(H2:H4)




result
FILE_2022.xlsx.xlsm
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/01/2022HASSONBTS00INV-001201202400
3201/01/2022HASSONBTS01INV-001501105500
4301/01/2022HASSONBTS02INV-0015022111050
5401/01/2022HASSONBTS03INV-001601227320
6BALANCE26270
7101/01/2022HUSSNIBTS01INV-002701208400
8201/01/2022HUSSNIBTS02INV-002801159200
9301/01/2022HUSSNIBTS00INV-002601126720
10BALANCE24320
SH
Cell Formulas
RangeFormula
H7:H9,H2:H5H2=F2*G2
H6H6=SUM(H2:H5)
H10H10=SUM(H7:H9)



FILE_2022.xlsx.xlsm
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/01/2022HASSON1BFFG1INVO-00201202400
3201/01/2022HASSON2BFFG2INVO-00501105500
4301/01/2022HASSON3BFFG3INVO-005022111050
5401/01/2022HASSON4BFFG4INVO-00601227320
6BALANCE26270
7101/01/2022HUSSNIBTS01INVO-01701208400
8201/01/2022HUSSNIBTS02INVO-01801159200
9301/01/2022HUSSNIBTS00INVO-01601126720
10BALANCE24320
mm
Cell Formulas
RangeFormula
H7:H9,H2:H5H2=F2*G2
H6H6=SUM(H2:H5)
H10H10=SUM(H7:H9)




FILE_2022.xlsx.xlsm
ABCDEFGH
1ITEMDATENAMEBATCHINV NOQTYPRICEBALANCE
2101/02/2022HASSONBVAC1AN001201222440
3201/02/2022HASSONBVAC2AN001101121120
4301/02/2022HASSONBVAC3AN001122222664
5BALANCE6224
6101/03/2022HOSSAMVBD1AN002101251250
7201/03/2022HOSSAMVBD2AN002101151150
8301/03/2022HOSSAMVBD3AN002201122240
9BALANCE4640
main
Cell Formulas
RangeFormula
H6:H8,H2:H4H2=F2*G2
H5,H9H5=SUM(H2:H4)




by the way
I just would use specific sheets , not all of sheets in workbook and should split to xlsm extension .
 
Upvote 0
by the way
I just would use specific sheets , not all of sheets in workbook and should split to xlsm extension .
Then you must indicate the names of those sheets.

Put the names on the next line of the macro:
VBA Code:
aryShs = Array("SH", "MM", "MAIN")

Another detail you didn't mention. If a sheet does not have a year, in your example the "Main" sheet does not have the year 2024, then the "Main" sheet should not be created in the 2024 workbook?
I guess not, but I shouldn't be assuming, you should be more specific, even if it's very obvious to you, I really don't know what you end up needing. It is for you to consider in future requests.

Try the following macro, It may be a little slow, it depends on the number of sheets, the number of records and the number of names you have on each sheet:
VBA Code:
Sub Split_File_Into_Multiple_Files()
  Dim wb1 As Workbook, wb2 As Workbook
  Dim sh As Worksheet, sh2 As Worksheet
  Dim ar As Range
  Dim dic As Object
  Dim aryShs As Variant, ky As Variant, a() As Variant
  Dim numSheets As Long, i As Long, n As Long

  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  Application.StatusBar = False
  numSheets = Application.SheetsInNewWorkbook
  
  Set wb1 = ThisWorkbook
  Set dic = CreateObject("Scripting.Dictionary")
  aryShs = Array("SH", "MM", "MAIN")
  
  For Each sh In wb1.Sheets(aryShs)
    a = sh.Range("A2", sh.Range("H" & Rows.Count).End(3)).Value
    For i = 1 To UBound(a, 1)
      If a(i, 3) <> "" Then
        dic(Year(a(i, 2))) = Empty
      End If
    Next
  Next

  Application.SheetsInNewWorkbook = UBound(aryShs) + 1
  For Each ky In dic.keys
    Application.StatusBar = "Creating book: " & ky
    Set wb2 = Workbooks.Add
    n = 0
    For Each sh In wb1.Sheets(aryShs)
      n = n + 1
      With wb2.Sheets(n)
        .Name = sh.Name
        sh.Range("A1:H1").Copy
        .Range("A1").PasteSpecial xlPasteAll
        .Range("A1").PasteSpecial xlPasteColumnWidths
        For Each ar In sh.Range("B2", sh.Range("B" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants).Areas
          If Year(ar.Cells(1).Value) = ky Then
            ar.Offset(, -1).Resize(ar.Rows.Count + 1, 8).Copy .Range("A" & Rows.Count).End(3)(2)
          End If
        Next
      End With
    Next
    For Each sh2 In wb2.Sheets
      If sh2.Range("A2").Value = "" Then sh2.Delete
    Next
    wb2.SaveAs wb1.Path & "\" & "FILE_" & ky, xlOpenXMLWorkbook
    wb2.Close False
  Next
  
  Application.SheetsInNewWorkbook = numSheets
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
  Application.StatusBar = False
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Respectfully
Dante Amor
----- --
😇
 
Upvote 0
Solution
Another detail you didn't mention. If a sheet does not have a year, in your example the "Main" sheet does not have the year 2024,
sorry !
then the "Main" sheet should not be created in the 2024 workbook?
that's correct !
everything is great!
much appreciated for your help.;)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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