Save Multiple Files Excel that has been automatically splitted by a column

Lidya

New Member
Joined
Feb 26, 2025
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello.
Does anyone knows how to solve this?
I have a master data , and i want to split the master data into multiple files which is splitted by a column.
For example :
Master data
UnitType AType BType C
202020212020202120202021
X100020002000200020002000
Y200010001000100040004000
X200010001000100040004000
X20003000030000100040004000
Y2000300001000100040004000
Z2000300004000400040004000
Z2000300004000400040004000

I want to split by column "UNIT", so there will be 3 excel files with files name "X.xls" , "Y.xls", and "Z.xls" and each files contains only the specific unit, like this:
For excel files name "X.xls", will contains only :
UnitType AType BType C
202020212020202120202021
X100020002000200020002000
X200010001000100040004000
X20003000030000100040004000

For excel files name "Y.xls", will contains only :

UnitType AType BType C
202020212020202120202021
Y200010001000100040004000
Y2000300001000100040004000

Can anyone knows how to solve?
I'm not familiar into any macros.. please guide me from the beginning..
thanks a lot!!
 
Welcome to Mr Excel.

Put this code into a main code module in the workbook containing the data.

If you have not got the Developer option on the ribbon then follow this.
Show the Developer tab - Microsoft Support

Select Developer, Visual Basic, Insert, Module to create a new module.

In the editing window, paste the VBA code.

Change the code to indicate the source worksheet and destination folder where indicated.

To run the code, select Developer, Macros on the ribbon and select 'subSplitData' and Run.

VBA Code:
Public Sub subSplitData()
Dim lngLastRow As Long
Dim WsMaster As Worksheet
Dim strPath As String
Dim arr() As Variant
Dim i As Integer

  ActiveWorkbook.Save
  
  ' Turn off screen updating.
  Application.ScreenUpdating = False

  ' Assign worksheet object.
  Set WsMaster = Worksheets("Master") '<<<<< --- CHANGE THE WORKSHEET NAME AS APPROPRIATE
  
  ' Set path where new workbooks are to be saved.
  strPath = ActiveWorkbook.Path & "\" ' <<<<< --- CHANGE IF THE DESTINATION PATH IS NOT TO BE THE SAME.

  ' Assertain the last row of the master worksheet.
  With WsMaster
    lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  End With
  
  ' Loop three times for X, Y and Z.
  For i = 1 To 3
    
    ' Use the worksheet FILTER function to populate an array with the X, Y or Z data.
    arr = Evaluate("FILTER(A2:G" & lngLastRow & ",A2:A" & lngLastRow & "=" & """" & Mid("XYZ", i, 1) & """" & ")")
    
    ' This creates a new single sheet workbook.
    WsMaster.Copy
      
    With ActiveWorkbook
      With .Sheets(1)
        .Name = Mid("XYZ", i, 1) ' Name the worksheet in the new workbook.
        .Range("A2:G" & Rows.Count).Cells.Clear   ' Clear all cells apart from the first row.
        .Range("A2:G" & UBound(arr) + 1).Value = arr ' Write the data in the array to the new worksheet.
      End With
      On Error Resume Next
      Kill (strPath & Mid("XYZ", i, 1) & ".xls") ' Delete the destination workbook if it exists.
      On Error GoTo 0
      .SaveAs strPath & Mid("XYZ", i, 1) & ".xls" ' Save the destination workbook.
      .Close True ' Close the destination workbook.
    End With
  
  Next i ' Loop to next value in X,Y or Z.
  
  ' Turn on screen updating.
  Application.ScreenUpdating = True
  
  MsgBox "New workbooks created.", vbOKOnly, "Confirmation."
  
End Sub
 
Upvote 0

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