• 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.

Split data in to multiple files as per cell value

dingdang

Member
Hi Guru's


I have data which i want to split into multiple files as per availabel user. if data is approx 7592 ( data and user wiil not fixed ) and avaible users are 8.


i want to split data in to 8 files of 949 row each with file name mentioned in column B


attached sample file for your ref. data is in sheet1 and user details are in sheet 2

Pls see the formula i have used in sheet2 in yellow color.


files should same in c:data folder


Pls help if we can do this with macro


http://dl.dropbox.com/u/66400357/data.xlsx


Thanks in advance.
 
Hi, dingdang!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the three first green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about your question...


1) If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords "split data multiple files" and press Search button. You'd retrieve many links from this website, like the following one(s), maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.

http://www.google.com/url?q=http://chandoo.org/forums/topic/split-data-in-an-excel-file-to-multiple-files&sa=U&ei=PsK7T5m4BYWQ6gGyp9TtCg&ved=0CAUQFjAA&client=internal-uds-cse&usg=AFQjCNGOnHv-w6oz0f5GOna1rkzfKxNfEw


2) Which is the criteria for identifying records (rows) to be sent to each file?


Regards!
 
Sir,


currently i am doing manualy calculation , no of records (7592) / users (8) = 949 rows for first file with file name A1 and so on...


for example

A1:G949 will be in first file then A950:G1898 will be in second file with file name A2
 
Hi, dingdang!


Paste this code into a new module and run macro "Splitting".

Adjust constant ksFileListTable to proper range.


-----

[pre]
Code:
Option Explicit

Sub Splitting()
' constants
Const ksMainDataWorksheet = "Main Data"
Const ksMainDataTable = "A:G"
Const ksFileListWorksheet = "File name"
Const ksFileListTable = "A4:C11"
Const ksExtension = ".xlsx"
' declarations
Dim rngData As Range, rngFile As Range
Dim I As Long, J As Long, K As Long
' start
Set rngData = Worksheets(ksMainDataWorksheet).Range(ksMainDataTable)
Set rngFile = Worksheets(ksFileListWorksheet).Range(ksFileListTable)
' process
I = 0
For J = 1 To rngFile.Rows.Count
K = rngFile.Cells(J, 3).Value
Range(rngData.Rows(I + 1), rngData.Rows(I + K)).Copy
Workbooks.Add
ActiveSheet.Paste Range("A1")
ActiveWorkbook.Close True, ThisWorkbook.Path & Application.PathSeparator & rngFile.Cells(J, 2) & ksExtension
I = I + K
Next J
' end
Set rngData = Nothing
Set rngFile = Nothing
End Sub
[/pre]
-----


Just advise if any issue.


Regards!


EDIT: Please note that file list in second sheets handles everything, so if you want to assign different number or rows to each file, just replace the formula in C column. It's up to you to check sum of rows per file against total rows, not in B1 cell but actually in first sheet. Same criteria for number of files against B2 cell.
 
Sir,


if i want to change path to save files, where should inclued it in macro.

i want to save files in c:data folder.
 
Hi, dingdang!


This part of the line "ThisWorkbook.Path & Application.PathSeparator & rngFile.Cells(J, 2) & ksExtension" builds the full path for the file, it starts with:

ThisWorkbook.Path : folder where is the actual file

Application.PathSeparator : back slash

rngFile.Cells(J, 2) : file name in your FileListTable

ksExtension : .xlsx


So you should insert/replace your wanted path before the back slash as:

"C:Data" & rngFile.Cells(J, 2) & ksExtension


Glad you solved it. Thanks for your feedback and for your kind words too. Welcome back whenever needed or wanted.


Regards!
 
Sir,


Thanks,

one more help required, while uploading the output file in my core system showing some error. after consult with out IT engg. output file extension should be as .txt instead of .xlsx.


Pls guide.
 
Hi, dingdang!


Try changing:


a) constant

Const ksExtension = ".xlsx"

by:

Const ksExtension = ".txt"


b) instruction

ActiveWorkbook.Close True, ThisWorkbook.Path & Application.PathSeparator & rngFile.Cells(J, 2) & ksExtension

by:

ActiveWorkbook.SaveAs ThisWorkbook.Path & Application.PathSeparator & rngFile.Cells(J, 2) & ksExtension, xlCSVWindows 'xlTextWindows or xlUnicodeText

ActiveWorkbook.Close False


Regards!


PS: and tell your IT guys (engineers or whatsoever) to not be so lazy...
 
Sir,


Need help again, i have fixed no of file name as 8 and subsequently set range in macro, but if i want to creat less than 8 files with 0 value in "no of row" coloumn, macro stuck at below line


"Range(rngData.Rows(I + 1), rngData.Rows(I + K)).Copy"


if i delete row A4 then i need every time to change range in macro.


for example if i put value against A1 and A5 file name as "0" then output file for A1 & A5 should not create.


Total Row : 7592

No of files : 8

Sr. no File name no of row

1 A1 0

2 A2 949

3 A3 949

4 A4 949

5 A5 0

6 A6 949

7 A7 949

8 A8 949


Plsss guide.
 
Hi, dingdang!


Try modifying the statements within the For...Next loop to:

-----

[pre]
Code:
K = rngFile.Cells(J, 3).Value
If K > 0 Then
Range(rngData.Rows(I + 1), rngData.Rows(I + K)).Copy
Workbooks.Add
ActiveSheet.Paste Range("A1")
ActiveWorkbook.Close True, ThisWorkbook.Path & Application.PathSeparator & rngFile.Cells(J, 2) & ksExtension
I = I + K
End If
[/pre]
-----


Regards!
 
Hi, dingdang!

Glad you solved it. Thanks for your feedback and for your kind words too. Welcome back whenever needed or wanted.

Regards!
 
Back
Top