Thursday, 9 July 2015

Split 1 Excel file into multiple Excel files based on data.

Requirement:
1. We have Sales Data dump with columns like- Order Number, Position, Customer, Item Name, Order Status.
2. We must create separate files for each Customer from this dump.

So lets say we create a file with the file name as <Customer Code>.xlsx and store all the records for that Customer into this new excel file. Similarly we create multiple files. Thus a new file created for each unique Customer.

Solution:
 I created the following Excel VBA Macro on the excel where full Sales Data dump is stored.

Sub details()
     Dim sh1 As Worksheet
     Dim newsh1 As Worksheet
     Dim newwb As Workbook
     Dim i, totalrows As Integer
     Dim j As Integer
     Dim newfile As String
     Dim sortcol As String
     Dim folderpath As String
     
     sortcol = InputBox("Enter the Column Number based on which u want to split the Excel. Eg. A")
     folderpath = InputBox("Enter path to store split files. Eg. C:\folderName\")
     
     Set sh1 = ThisWorkbook.Sheets("Sheet1")
     sh1.Range(sortcol + "1").Sort Key1:=sh1.Range(sortcol + "1"), Order1:=xlAscending, Header:=xlNo
     totalrows = sh1.Range(sortcol + "1", sh1.Range(sortcol + "1").End(xlDown)).Rows.Count
     newfile = ""
     j = 0
     
     For i = 1 To totalrows

        If newfile = sh1.Range(sortcol + CStr(i)) Then
               j = j + 1
               newsh1.Range("A" + CStr(j) + ":Z" + CStr(j)) = sh1.Range("A" + CStr(i) + ":Z" + CStr(i)).Value
        Else
               If newfile <> "" Then
                     newwb.Save
                     newwb.Close
               End If
               newfile = sh1.Range(sortcol + CStr(i)).Value
               Set newwb = Workbooks.Add
               j = 0
               With newwb
                 .SaveAs Filename:=folderpath + newfile + ".xlsx"
                 Set newsh1 = .Sheets("Sheet1")
               End With
               
               j = j + 1
               newsh1.Range("A" + CStr(j) + ":Z" + CStr(j)) = sh1.Range("A" + CStr(i) + ":Z" + CStr(i)).Value
        End If
     Next i
    If newfile <> "" Then
        newwb.Save
        newwb.Close
    End If
  End Sub
      
This macro is dynamic enough to handle any similar excel requirement of yours.
Usage-
1. The Sales Data dump excel file should not have any column headers.
2. When you run this. You will be asked to enter the Column name based on what you want to split the excel file. So in our case I will enter value "C" as my Customer Code is present in Cth column.
3. Then it will ask for the folder name where you want the newly generated split files to be stored.
I entered C:\temp\

3 comments:

  1. It will be more helpful if you post the Exchange Schema related information and example.

    Thanks for the making us BAAN aware.

    ReplyDelete
  2. Sure Keyur, will write a blog on exchange scheme for sure. Was occupied with an implementation all these days.... Couldn't contribute to the blog.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete