• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Spliting Excel File into Many

venky24

New Member
I got a datafile which contains REGION and SALESMAN. I want to split these into seperate workbook on the basis of REGION .... AND... within this REGION, the SALESMAN data should be in different worksheet. So suppose for Region A, there are 3 salesman in the data, the code should create a workbook by name REGION A and then 3 worksheets within in for the 3 salesman. How should i go about. (Rest of the requirement are like worksheet tab name should be salesman's name, File name should be REGION)


There is a vba code for spliting excel file into many

http://chandoo.org/wp/2011/10/19/split-excel-file-into-many/


but that does not complete my requirement. Please help., as this would save me around 1 hours time daily....
 
Hi venky24,


I am also new to VBA and in the very initial phase of learning. However, I have tried to answer your question as follows:


I have taken the data from the link (http://chandoo.org/wp/2011/10/19/split-excel-file-into-many/) you have mentioned above.


There are four salesmen in that data-Joseph, Maria, Matt and Lawrence.Entirre data ranges from A1 to G1080.


I would request you to do the following:


i)Create an empty workbook and name it as "Region"(as you preferred) and close it.


ii)Place the four salesmen from L2 to L5 in the workbook ("break-data-example.xlsm") that actually contain the data


iii) In this workbook: press alt+f11 to open Visual Basic Editor.Insert a module and copy paste the following code


Sub CreateNewData()


Application.ScreenUpdating = False

Application.DisplayAlerts = False


Dim lstrow As Long

Dim i As Integer

Dim j As Integer

Dim tempname As String

Dim curWorkbook As Workbook

Dim wb1 As Workbook


i = 2

j = 1


lstrow = ThisWorkbook.Worksheets("Sheet1").Range("L" & Rows.Count).End(xlUp).Row


Do While Range("L" & (i)).Value <> ""

tempname = Range("L" & (i)).Value


Set wb1 = Workbooks.Open("C:Documents and Settingskaushik DuttaDesktopRegion.xlsx").....''remember to change the path in this line of code where you have saved your workbook(Region)''


Worksheets.Add


Workbooks("break-data-example.xlsm").Activate

Range("A1").Select

Selection.AutoFilter

ActiveSheet.Range("$A$1:$G$1080").AutoFilter Field:=2, Criteria1:=tempname

Range(Selection, Selection.End(xlToRight)).Select

Range(Selection, Selection.End(xlDown)).Select

Selection.Copy

wb1.Activate


Worksheets("Sheet" & (j)).Select


Range("A1").Select

ActiveSheet.Paste

Sheets("Sheet" & (j)).Name = tempname

wb1.Save


i = i + 1

j = j + 1

Workbooks("break-data-example.xlsm").Activate

Sheets("Sheet1").Select

Loop


wb1.Activate


Dim sh As Variant


On Error GoTo Exits:


For Each sh In Sheets

If Not IsChart(sh) Then

If Application.WorksheetFunction.CountA(sh.Cells) = 0 Then sh.Delete

End If

Next sh


Exits:

Application.ScreenUpdating = True

Application.DisplayAlerts = True


End Sub


Public Function IsChart(sh) As Boolean

Dim tmpChart As Chart

On Error Resume Next

Set tmpChart = Charts(sh.Name)

IsChart = IIf(tmpChart Is Nothing, False, True)

End Function


It works for.


Let me know if you face any challenge.


Regards,

Kaushik
 
thanks kaushik03 for your efforts. the code created 4 files Joseph, Maria, Matt and Lawrence... but with Joseph i wanted region based worksheets. Let me give an example


REGION-----------SALESMAN-----XXXX------XXXX---------XXXX

NORTH------------JOSEPH--------XXXX------XXXX--------XXXXX

NORTH------------JOSEPH-------XXXX-------XXXX-------XXXXX

SOUTH------------JOSEPH------XXXXX------XXXXX--------XXXX

WEST------------MARIA--------XXXXX-------XXXXX------XXXX


So in this case there will be 2 files JOSEPH and MARIA. But when i open JOSEPH, i should have two worksheets in it NORTH and SOUTH (containing respective records)


Hope i able to put my thoughts clearly...
 
Ok....make sense. Thank you for clarifying. My apologies!I just misunderstood your requirement earlier and wrote the code accordingly.


So you want separate workbook for each salesman with the number of sheets according to the regions they are aligned to(one sheet for each region).


I am little busy now. But I will sit sometimes today to write the code and post the same by tomorrow.


Thank you for your patience.


Kaushik
 
Hi Vinky,


You can have a look at ASAP Utilities at http://www.asap-utilities.com/ This has lot more than you are asking.


Regards,

Kuldeep
 
Back
Top